我正在尝试使用 python 和 tkinter 制作一个程序来运行已在复选框中选中的程序。
import sys
from tkinter import *
import tkinter.messagebox
def runSelectedItems():
if checkCmd == 0:
labelText = Label(text="It worked").pack()
else:
labelText = Label(text="Please select an item from the checklist below").pack()
checkBox1 = Checkbutton(mGui, variable=checkCmd, onvalue=1, offvalue=0, text="Command Prompt").pack()
buttonCmd = Button(mGui, text="Run Checked Items", command=runSelectedItems).pack()
这是代码,但我不明白为什么它不起作用?
谢谢。
最佳答案
您需要为变量使用 IntVar
:
checkCmd = IntVar()
checkCmd.set(0)
def runSelectedItems():
if checkCmd.get() == 0:
labelText = Label(text="It worked").pack()
else:
labelText = Label(text="Please select an item from the checklist below").pack()
checkBox1 = Checkbutton(mGui, variable=checkCmd, onvalue=1, offvalue=0, text="Command Prompt").pack()
buttonCmd = Button(mGui, text="Run Checked Items", command=runSelectedItems).pack()
在其他新闻中,成语:
widget = TkinterWidget(...).pack()
不是一个很好的。在这种情况下,widget
将始终为None
,因为这是 Widget.pack()
返回的内容。一般来说,您应该创建您的小部件并通过 2 个单独的步骤使其了解几何管理器。例如:
checkBox1 = Checkbutton(mGui, variable=checkCmd, onvalue=1, offvalue=0, text="Command Prompt")
checkBox1.pack()
关于python - 从 python tkinter 中的复选框获取输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16285056/