I following code I create a window with just a checkbox and a button. When I get value of the checkbox by executing just the checkbox window, everything goes fine. When I construct that window from another window, checkbox behaves unexpectedly. Feel free to run following code. Result I get is:
1
0
I just don't get it why the second result is 0 and not 1. Any ideas?
import tkinter as tk
from tkinter import Tk, Button, Checkbutton
class SecondaryWindow:
def __init__(self, window):
self.window = window
self.decimal_comma = tk.IntVar()
Checkbutton(window, variable = self.decimal_comma).grid(row = 0, column = 0)
Button(window, text = "Check the checkbox and click me", command = self.ok_pressed).grid(row = 1, column = 0)
def ok_pressed(self, event = None):
print(self.decimal_comma.get())
self.window.destroy()
class MainWindow(Tk):
def __init__(self):
super().__init__()
Button(self, text = "Click me", command = self.popup).pack(side=tk.LEFT)
def popup(self, event = None):
loader = SecondaryWindow(Tk())
loader.window.wait_window()
self.destroy()
def main1():
MainWindow().mainloop()
def main2():
top = Tk()
loader = SecondaryWindow(top)
loader.window.wait_window()
top.mainloop()
if __name__ == "__main__":
main2()
main1()
Aucun commentaire:
Enregistrer un commentaire