2017-11-08 2 views
-2

私のチェックボタンに何をしても、変数を設定していないようです。 はここで関与しているコードの一部です:tkinter checkbuttonが変数を設定していない

class Window: 
    def __init__(self): 
     self.manualb = 0 #to set the default value to 0 

    def setscreen(self): 
     #screen and other buttons and stuff set here but thats all working fine 
     manual = tkr.Checkbutton(master=self.root, variable=self.manualb, command=self.setMan, onvalue=0, offvalue=1) #tried with and without onvalue/offvalue, made no difference 
     manual.grid(row=1, column=6) 

    def setMan(self): 
     print(self.manualb) 
     #does some other unrelated stuff 

それはちょうど0アム私が何か間違ったことを印刷し続けますか?マニュアル以外に何もしません。

答えて

2

あなたはIntVar()

IntVar()を探しているのは、あなたがそれを割り当てるウィジェットの値を保持しますget()と呼ばれる方法があります。

この特定のインスタンスでは、1または0(オンまたはオフ)になります。 これは次のようなものです:

from tkinter import Button, Entry, Tk, Checkbutton, IntVar 

class GUI: 

    def __init__(self): 

     self.root = Tk() 

     # The variable that will hold the value of the checkbox's state 
     self.value = IntVar() 

     self.checkbutton = Checkbutton(self.root, variable=self.value, command=self.onClicked) 
     self.checkbutton.pack() 

    def onClicked(self): 
     # calling IntVar.get() returns the state 
     # of the widget it is associated with 
     print(self.value.get()) 

app = GUI() 
app.root.mainloop() 
+1

こんにちは@Jebby。誰かがなぜ自分のプログラムで何かをしている/してはいけない/してはいけないのかを説明するのは良い考えです。これにより、あなたが説明したアイデアや言語を新入者が簡単に理解できるようになります。 –

+1

ありがとう@EthanField私は自分の記事を編集して、IntVarの情報を少し追加しました。 – Jebby

2

これは、tkinterのvariable classesのいずれかを使用する必要があるためです。

これは以下のようになります。基本的に

from tkinter import * 

root = Tk() 

var = IntVar() 

var.trace("w", lambda name, index, mode: print(var.get())) 

Checkbutton(root, variable=var).pack() 

root.mainloop() 

IntVar()は、それが割り当てられているウィジェットの値「を保持する」と「コンテナ」非常にが緩く話す)です。

関連する問題