Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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 ;)

like image 871
nieka Avatar asked Nov 05 '15 12:11

nieka


Video Answer


1 Answers

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

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

print C1.cget("text")
print C2.cget("text")
like image 50
Kevin Avatar answered Oct 09 '22 12:10

Kevin