python - 从 python tkinter 中的复选框获取输入?

标签 python input checkbox tkinter

我正在尝试使用 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/

相关文章:

python - 使用 Python 从字符串中提取整数

Android 上的 Javascript onKeyPress 键代码始终为 0

javascript - 如何在 Laravel 5.2 中使用 JavaScript 填充复选框

javascript - 多个复选框中的一个应始终保持未选中状态

checkbox - React Checkbox 不发送 onChange

python - 使用 python 获取 youtube 数据

python - 使用 Displacy (Python) 可视化依赖解析

javascript - 避免通过动态输入(HTML - Jquery)发送空值

javascript - 默认添加复选框值

python - 在具有共享 Windows 文件夹的 VMWare 中使用 virtualenv