0

im trying to use classes to create multiple checkboxes in tktinter python, but for some reason the checkboxes aren't showing, this is the error i'm getting AttributeError: 'crop' object has no attribute 'tk' And heres my code

from tkinter import *
from tkinter.ttk import *
import tkinter as tk  # python 3
screen = Tk()

class crop:

    def __init__(self,sizename,sizevar,onvar):
        sizevar= StringVar
        self.tk.Checkbutton(screen, text=sizename, variable=sizevar, onvalue=onvar, offvalue='no')
        self.pack()


def main():
    first = crop('2.5','twofive','twofiveon')
    second = crop('3','three','threeon')
screen.mainloop()    
main()

thanks for your help in advance.

2 Answers 2

1

There are several things wrong with your code.

First, you need to call mainloop after you call main, not before.

Second, sizevar= StringVar needs to be sizevar= StringVar().

Third -- and this is the line that is throwing the error -- you are trying to call self.tk.Checkbutton but your crop object hasn't defined an attribute named tk. I'm not entirely sure what you are trying to accomplish, but my guess is that you are just trying to call the tk checkbutton which means the code should be tk.Checkbutton(...)

The next problem is with self.pack() - self is the crop object. It is not a widget so it doesn't have a pack method. It appears that your intention is to pack the checkbutton, so you need to save a reference to the checkbutton and then call pack on it (eg: self.cb = tk.Checkbutton(...); self.cb.pack())

Another problem is that you're importing tkinter twice. You should only do it once. You need to remove both from tkinter import * and from tkinter.ttk import *. Then, use tk. or ttk. as a prefix when referring to tkinter and ttk objects.

A final issue is that you're not following PEP8 guidelines, making your code harder for other programmers to understand. Specifically, crop needs to be Crop.

Putting that all together, your code should1 look like this:

import tkinter as tk
from tkinter import ttk

screen = tk.Tk()

class Crop:

    def __init__(self,sizename,sizevar,onvar):
        sizevar= tk.StringVar()
        self.cb = tk.Checkbutton(screen, text=sizename, variable=sizevar, onvalue=onvar, offvalue='no')
        self.cb.pack()


def main():
    first = Crop('2.5','twofive','twofiveon')
    second = Crop('3','three','threeon')

main()
screen.mainloop()

1 I say should because it's not 100% clear what you want to do. Because of the way you originally did the imports it's unclear if you're intending to use the ttk version of the Checkbutton widget or the tk version. This is one of the reasons why wildcard imports are bad when importing tkinter. If you want to use the ttk checkbutton, you need to change that one line to this:

self.cb = ttk.Checkbutton
Sign up to request clarification or add additional context in comments.

1 Comment

Wow Thankyou so much, that was a really good explanation, to be honest I don’t know the difference between tk and Ttk I was just following a tutorial I was watching, but thanks
0

You need to do something like:

  def __init__(self,sizename,sizevar,onvar):
        global screen # or pass it as __init__ parameter
        sizevar= StringVar
        b= tk.Checkbutton(screen, text=sizename, variable=sizevar, onvalue=onvar, offvalue='no')
        b.pack()

4 Comments

that just takes away the use of classes, i need to use classes to shorten my code as i will be making like 20 checkboxes, thanks for your help though
No it does not. You need to identify what you want to retrieve from each crop instance and make it a property of the class eg self.sizevar = StringVar
im very confused could you show me in full code please? thanks
There's no need to declare screen as global since they aren't trying to modify the variable. A global variable is readable inside functions without declaring them global. Declaring it might be a good practice, but that doesn't answer the OP's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.