Question

I'm not sure why this isn't working, I've tried changing the order of things and stuff but it's still not working. The point of the code is so show the word Null and when you select any of the checkbuttons, depending on if its an odd or even amount, its will change the word Null to Odd or Even. Any help would be good.

# Create a window
the_window = Tk()

# Give the window a title
the_window.title('Odd or Even')


##Some global variables
margin = 8
text = 'Null'

##Changing the size of the window
the_window.geometry("350x150")

##Making the label frame
labelframe = LabelFrame(the_window, bd = 3)
labelframe.grid(columnspan = 3, padx = margin, pady = margin)

##Adding the Label
changeable_label = Label(labelframe, text = text, font = ('Arial', 60),
                           fg = ('black'))
changeable_label.pack(side = TOP)

##Addind the Check Buttons

def odd_even():
    chk_btn_value = value1 + value2 + value3

    if chk_btn_value == 0:
        text = 'Null'
    elif chk_btn_value % 2 != 0:
        text = 'Odd'    
    else:
        text = 'Even'

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()

value1 = var1.get()
value2 = var2.get()
value3 = var3.get()


alpha = Checkbutton(the_window, text = 'Alpha', variable = var1,
                    onvalue = 1, offvalue = 0, command = odd_even)
alpha.grid(row = 1, column = 1)

beta = Checkbutton(the_window, text = 'Beta', variable = var2,
                    onvalue = 1, offvalue = 0, command = odd_even)
beta.grid(row = 1, column = 2)

gamma = Checkbutton(the_window, text = 'Gamma', variable = var3,
                    onvalue = 1, offvalue = 0, command = odd_even)
gamma.grid(row = 1, column = 3)


# Start the event loop
the_window.mainloop()
Was it helpful?

Solution

You need to get their values everytime you call the function because they are changing everytime you check or uncheck. Also to update label's text, you need to use something like this. label_name["text"] = "new_text"

Additional to that, Checkbutton's default on and off values are 1 and 0 respectively. You don't need to write those.

Applying all these to your code, will result this.

from Tkinter import *

def odd_even():
    value1 = var1.get()
    value2 = var2.get()
    value3 = var3.get()
    chk_btn_value = value1 + value2 + value3

    if chk_btn_value == 0:
        changeable_label['text'] = "Null"
    elif chk_btn_value % 2 != 0:
        changeable_label['text'] = "Odd"
    else:
        changeable_label['text'] = "Even"

# Create a window
the_window = Tk()

# Give the window a title
the_window.title('Odd or Even')


##Some global variables
margin = 8
text = 'Null'

##Changing the size of the window
the_window.geometry("350x150")

##Making the label frame
labelframe = LabelFrame(the_window, bd = 3)
labelframe.grid(columnspan = 3, padx = margin, pady = margin)

##Adding the Label
changeable_label = Label(labelframe, text = text, font = ('Arial', 60),
                           fg = ('black'))
changeable_label.pack(side = TOP)

##Addind the Check Buttons

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()

alpha = Checkbutton(the_window, text = 'Alpha', variable = var1,
                     command = odd_even)
alpha.grid(row = 1, column = 1)

beta = Checkbutton(the_window, text = 'Beta', variable = var2,
                     command = odd_even)
beta.grid(row = 1, column = 2)

gamma = Checkbutton(the_window, text = 'Gamma', variable = var3,
                     command = odd_even)
gamma.grid(row = 1, column = 3)


# Start the event loop
the_window.mainloop()

OTHER TIPS

You have to call the .get() method in the callback function. Also try the changeable_label.set("New Text!") to change the text of the Label. Effbot has very good resources for tkinter: http://effbot.org/tkinterbook/label.htm

from Tkinter import *
# Create a window
the_window = Tk()

# Give the window a title
the_window.title('Odd or Even')


##Some global variables
margin = 8
text = 'Null'

##Changing the size of the window
the_window.geometry("350x150")

##Making the label frame
labelframe = LabelFrame(the_window, bd = 3)
labelframe.grid(columnspan = 3, padx = margin, pady = margin)

##Adding the Label
changeable_label = Label(labelframe, text = text, font = ('Arial', 60),
                           fg = ('black'))
changeable_label.pack(side = TOP)

##Addind the Check Buttons

def odd_even():
    value1 = var1.get()
    value2 = var2.get()
    value3 = var3.get()
    chk_btn_value = value1 + value2 + value3
    print chk_btn_value

    if chk_btn_value == 0:
        text = 'Null'
    elif chk_btn_value % 2 != 0:
        text = 'Odd'    
    else:
        text = 'Even'

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()




alpha = Checkbutton(the_window, text = 'Alpha', variable = var1,
                    onvalue = 1, offvalue = 0, command = odd_even)
alpha.grid(row = 1, column = 1)

beta = Checkbutton(the_window, text = 'Beta', variable = var2,
                    onvalue = 1, offvalue = 0, command = odd_even)
beta.grid(row = 1, column = 2)

gamma = Checkbutton(the_window, text = 'Gamma', variable = var3,
                    onvalue = 1, offvalue = 0, command = odd_even)
gamma.grid(row = 1, column = 3)


# Start the event loop
the_window.mainloop()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top