0

I am new with Tkinter and Python. I am working with some checkboxes and button functions. I want the output to be output: 0, output: 1,... (0 is when the checkbox is not ticked and 1 is when the checkbox is ticked). The output is printed whenever the Submit button is pressed. Since at this moment, it is only printed once at the beginning

I just wonder if there is any a way to do that

Thank you and have a nice day :)

import tkinter as tk
from tkinter import *

root = tk.Tk()

class Blah:
    def __init__(self):
       self.varAll = IntVar()

    def Check(self):   
        but1 = Checkbutton(root, text='Selected', variable = self.varAll)
        but1.pack(side=LEFT, pady=4)

        b2 = tk.Button(root, text='Submit', command= self.State)
        b2.pack(side=tk.LEFT, padx=5, pady=15)
        b2.wait_variable(self.varAll)

    def State(self):
        print('State: ', self.varAll.get())
        self.state = self.varAll.get()
        return self.state

def Main():
    listt = Blah()
    listt.Check()
    #root.wait_variable(listt.varAll)
    print("output ", listt.varAll.get())

if __name__ == '__main__':
    Main()
quynhhgoogoo
  • 63
  • 1
  • 10
  • Read about [this](https://stackoverflow.com/questions/8683217/when-do-i-need-to-call-mainloop-in-a-tkinter-application).FrainBr33z3's answer is a nearly right answer. Just delete ``print("output ", listt.varAll.get())``.Then it is what you want. – jizhihaoSAMA Mar 10 '20 at 14:13

2 Answers2

0

You are missing a .mainloop. Add it in the Main function after the print statement as:

def Main():
    listt = Blah()
    listt.Check()
    #root.wait_variable(listt.varAll)
    print("output ", listt.varAll.get())
    root.mainloop()
FrainBr33z3
  • 1,085
  • 1
  • 6
  • 12
  • Thank you for the reply. I have tried it. However, it still does not print out the output as I wish :( I want whenever the Submit button is pressed, State() will update the new value and Main() will print the new one out :( – quynhhgoogoo Mar 10 '20 at 13:49
  • Better move `root = tk.Tk()` into `Main()` as well and remove `b2.wait_variable(self.varAll)` in `Check()`. – acw1668 Mar 10 '20 at 14:20
0

You can get that output by adding an "observer" callback to your IntVar, which will automatically be called whenever its value is changed — see the article The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar).

Here's how I would modify your code to use it. Note the added Notify() method. I also commented-out a number of things I don't think you need. There's no reason for State() to return anything since it will only be called by tkinter, and those wait_variable() calls aren't needed, either (if you explicitly call mainloop() yourself).

import tkinter as tk
from tkinter import *

class Blah:
    def __init__(self):
       self.varAll = IntVar()
       self.varAll.trace('w', self.Notify)

    def Check(self):
        but1 = Checkbutton(root, text='Selected', variable=self.varAll)
        but1.pack(side=LEFT, pady=4)

        b2 = tk.Button(root, text='Submit', command=self.State)
        b2.pack(side=tk.LEFT, padx=5, pady=15)
#        b2.wait_variable(self.varAll)

    def State(self):
        print('State: ', self.varAll.get())
        self.state = self.varAll.get()
#        return self.state

    def Notify(self, *args):
        print("output ", self.varAll.get())


def Main():
    listt = Blah()
    listt.Check()
#    root.wait_variable(listt.varAll)
#    print("output ", listt.varAll.get())
    root.mainloop()

if __name__ == '__main__':
    root = tk.Tk()
    Main()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thank you a lot for the informative comment! It solved my problem :D I just wonder if there is any way to print the "output" only when the Submit button is pressed :D it could really nice if I could do it :) Thank you in advanced. – quynhhgoogoo Mar 10 '20 at 18:55
  • You're welcome. You could add a `print()` call to `State()` or just call `self.Notify()` from it. – martineau Mar 10 '20 at 19:22