简体   繁体   English

如何从python中的checkbutton获取文本? (Tkinter)

[英]How to get the text from a checkbutton in python ? (Tkinter)

My code looks something like this我的代码看起来像这样

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

But I also want to print the text of the Checkbutton.但我也想打印 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

Thank you in advance ;)先感谢您 ;)

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

To get the value of a widget's attribute, try using cget .要获取小部件属性的值,请尝试使用cget

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM