我不确定为什么这不起作用,我已经尝试改变事物和事物的顺序,但它仍然无法正常工作。代码的点是这样显示单词Null,当你选择任何一个检查按钮时,根据它是奇数还是偶数,它会将单词Null改为Odd或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
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()
答案 0 :(得分:0)
每次调用函数时都需要获取它们的值,因为每次检查或取消选中它们时都会发生变化。另外,要更新标签文本,您需要使用类似的内容。 label_name["text"] = "new_text"
除此之外,Checkbutton
的默认开启和关闭值分别为1和0。你不需要写那些。
将所有这些应用到您的代码中,将会产生这种结果。
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()
答案 1 :(得分:-1)
您必须在回调函数中调用.get()方法。还可以尝试使用changeable_label.set(“New Text!”)来更改Label的文本。 Effbot为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()