从python tkinter中的复选框获取输入? [英] Get the input from a Checkbox in python tkinter?

查看:1491
本文介绍了从python tkinter中的复选框获取输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我想使用python和tkinter来创建一个程序来运行在复选框中选择的程序。 > import sys
from tkinter import *
import tkinter.messagebox
def runSelectedItems():
if checkCmd == 0:
labelText = Label(text =It工作)。pack()
else:
labelText = Label(text =请从下面的清单中选择一个项目)pack()
$ b 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 )
def runSelectedItems():
如果checkCmd.get()== 0:
labelText = Label(text =It works)pack()
else:
labelText = Label(text =请从下面的清单中选择一个项目)pack()

checkBox1 = Checkbutton(mGui,variable = checkCmd,onvalue = 1,offvalue = text =Command Prompt)。pack()
buttonCmd = Button(mGui,text =Run Checked Items,command = runSelectedItems).pack()
pre>




在其他消息中,惯用语:

  widget = TkinterWidget(...)。pack()

不是一个很好的一个。在这种情况下, widget 总是 Widgets.pack()。一般来说,你应该创建你的小部件,并让它知道几何管理器在两个单独的步骤。例如:

  checkBox1 = Checkbutton(mGui,variable = checkCmd,onvalue = 1,offvalue = 0,text =Command Prompt )
checkBox1.pack()


I am trying to use python and tkinter to make a program that run programs that have been selected in a check box.

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()

That is the code but I don't understand why it doesn't work?

Thanks.

解决方案

You need to use an IntVar for the variable:

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()


In other news, the idiom:

widget = TkinterWidget(...).pack()

Is not a very good one. In this case, widget will always be None since that is what is returned by Widget.pack(). In general, you should create your widget and make it aware of the geometry manager in 2 separate steps. e.g.:

checkBox1 = Checkbutton(mGui, variable=checkCmd, onvalue=1, offvalue=0, text="Command  Prompt")
checkBox1.pack()

这篇关于从python tkinter中的复选框获取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆