2014-09-03 61 views
1

請幫忙!正如它在標題中所說 - 我無法獲得複選框'變量值。Python 3.x tkinter - 無法獲取複選框'變量值

def init_widgets(self): 
    ttk.Button(self.root, command=self.insert_txt, text='Button', width='10').place(x=10, y=10) 
    ... 
    cbcc = ttk.Checkbutton(root, text="Damage", onvalue="on", offvalue='off').place(x=235, y=12) 
    ... 

    def insert_txt(self): 
    ... 
    cbcd = StringVar() 
    cbcd.get() 
    print(cbcd)    

    if cbcd == "on": 
     self.damage 
    else: 

打印提供了「PY_VAR2」,並與我點擊,每次(PY_VAR3等)

回答

0

似乎在你的代碼從那裏計數,在你Checkbutton參數「變」是無界,此外,要從IntVar或StringVar中獲取值(IntVar.get(),StringVar()。get()),您可以使用下面的代碼作爲示例使用Checkbutton小部件。

''' 
CheckBox Test 
References: 
http://effbot.org/tkinterbook/checkbutton.htm 
''' 

from tkinter import * 

class App: 

    def __init__(self, master): 
     self.var = IntVar() #This is the variable bounded to checkbutton to 
          #get the checkbutton state value 
     # 
     frame = Frame(master)   
     frame.pack() 

     self.checkbutton = Checkbutton(frame, text="Hello Checkbutton", 
             command=self.say_hello, variable=self.var) 
     self.checkbutton.pack(side=LEFT) 
     # 
     self.button = Button(frame, text="QUIT", fg="red", command=frame.quit) 
     self.button.pack(side=LEFT)  

    def say_hello(self): 
     ''' 
     Function Bounded to Checkbutton in command parameter, every click 
     either check or un-check print the current state of the checkbutton 
     '''   
     print("State Changed:", self.var.get()) 


if __name__ == '__main__': 
    root = Tk() 
    app = App(root) 
    # 
    root.mainloop() 
    root.destroy() # Quit the App when you click "Quit" 

在支票上的每個按鈕的點擊,你會看到印在控制檯的checkbutton當前狀態的值。您可以查看下一個參考,以獲得tkinter小部件的總體概述:http://effbot.org/tkinterbook/checkbutton.htm

我希望此代碼段可以幫助您。 非常好的問候。

+0

哇,謝謝,幫了很多! :)感謝澄清這是如何工作和最好的問候! – moupy 2014-09-03 16:55:20