如何从python中的checkbutton获取文本?(Tkinter) [英] How to get the text from a checkbutton in python ? (Tkinter)

查看:167
本文介绍了如何从python中的checkbutton获取文本?(Tkinter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码看起来像这样

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tkinter.Tk()
CheckVar1 = IntVar()
CheckVar2 = IntVar()

C1 = Checkbutton(top, text = "Music", variable = CheckVar1, onvalue = 1,  offvalue = 0 )
C2 = Checkbutton(top, text = "Video", variable = CheckVar2,onvalue = 1, offvalue = 0 )

我创建了两个带有值和一些文本的复选按钮.如果我想打印复选按钮的值,我使用以下代码:

I have created two checkbuttons with values and some text. If I want to print the values of the checkbuttons i use this code:

print CheckVar1.get()
print CheckVar2.get()

但我也想打印 Checkbutton 的文本.我尝试了以下操作:

But I also want to print the text of the Checkbutton. I tried to the following:

print C1.get("text")
print C2.get("text)

哪个根本不起作用.有什么技巧可以解决这个问题吗?或者我是否必须创建一些像这样的解决方法:(这看起来很奇怪)

Which does not work at all.Is there some trick for this? Or do I have to create some workaround like this: (Which seems quite strange)

 CheckVar1 = IntVar()
 CheckVar2 = IntVar()
 Name1 =  StringVar(value="Music")
 Name2 =  StringVar(value="Video")

 C1 = Checkbutton(top, text = Name1, variable = CheckVar1, onvalue = 1,  offvalue = 0 )
 C2 = Checkbutton(top, text = Name2, variable = CheckVar2,onvalue = 1, offvalue = 0 )

print Name1
print Name2

先谢谢你;)

推荐答案

print C1.get("text")
print C2.get("text")

要获取小部件属性的值,请尝试使用 cget.

To get the value of a widget's attribute, try using cget.

print C1.cget("text")
print C2.cget("text")

这篇关于如何从python中的checkbutton获取文本?(Tkinter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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