我使用tkinter在python 3.5中编写了一个代码,我在从复选框的当前状态(True / False)中获取正确的值时遇到了问题。 出于某种原因,我并不总是正确的价值。 这是产生问题的代码的一小部分:
import tkinter as tk
from tkinter import messagebox
# go button event listener
def goButtonCallBack():
global show_skeleton_whole_body, show_skeleton_no_legs
show_skeleton_whole_body = var_whole_body.get()
show_skeleton_no_legs = var_no_legs.get()
if show_skeleton_whole_body==False and show_skeleton_no_legs==False:
messagebox.showinfo("Try again", "You must choose one of the options!")
return
if __name__ == '__main__':
top = tk.Tk()
top.geometry("650x550")
var_whole_body = tk.IntVar()
checkbox_whole_body = tk.Checkbutton(top, text = "Whole Body", variable = var_whole_body, \
onvalue = True, offvalue = False, height=5, \
width = 200)
show_skeleton_whole_body = var_whole_body.get()
print ('main before select: show_skeleton_whole_body')
print (show_skeleton_whole_body)
checkbox_whole_body.select()
show_skeleton_whole_body = var_whole_body.get()
print ('main after select: show_skeleton_whole_body')
print (show_skeleton_whole_body)
checkbox_whole_body.pack(side=tk.LEFT)
checkbox_whole_body.place(x=15, y=200, width=100)
var_no_legs= tk.IntVar()
checkbox_no_legs = tk.Checkbutton(top, text = " No Legs", variable = var_no_legs, \
onvalue = True, offvalue = False, height=4, \
width = 200)
show_skeleton_no_legs = var_no_legs.get()
print ('main before select: show_skeleton_no_legs')
print (show_skeleton_no_legs)
checkbox_no_legs.select()
show_skeleton_no_legs = var_no_legs.get()
print ('main after select: show_skeleton_no_legs')
print (show_skeleton_no_legs)
checkbox_no_legs.pack(side=tk.LEFT)
checkbox_no_legs.place(x=15, y=250, width=100)
# create the 'go' button
bluebutton = tk.Button(top, text="go! >", fg="blue", command=goButtonCallBack)
bluebutton.place(x=90, y=400)
# instruction
instructionsString = "bla bla bla"
instructionsLabel = tk.Label(top, text=instructionsString, width=500, font=("Helvetica", 9))
instructionsLabel.pack()
instructionsLabel.place(x=15, y=450, width=500)
top.mainloop()
print('This is the end of the program!!')
由于某种原因,select()命令没有将值从0更改为1。 如果你运行代码并按下“去”。按钮会出现一个消息框,说明应该检查其中一个复选框但是它们都被检查了! 谁知道问题是什么?
由于 大卫