1

I'm trying to get data from a Toplevel window with Tkinter into a full Tk window.

I have my start menu Start.py that contains the class Start. The Start menu has a button which leads to a map editor in another window

self.config_4 = tk.Radiobutton(self.gamemode, text="Custom", value = 4, variable = val, command = self.set_custom)

which leads to

def set_custom(self):
    self.editor = Editor(self.menu)
    self.board = self.editor.getBoard()
    self.board = Board(BoardType.CLASSIC)

Editor is another file that opens a Toplevel window and contains the attribute self.board which I want to receive in Start.py

The destroy function stops the rest of the execution, and quit too. I tried using grab_set and wait for the window but without mainloop the editor does not work properly.

So basically the only thing I want is to get self.board from Editor to Start, and then from Start to main. Both of these is the same destroy problem.

Thanks !

#First.py
import tkinter as tk
from Test import Second


class First:
    def __init__(self):
        self.window = tk.Tk()
        self.test = tk.Button(self.window, command = self.popup, text="Test me").pack()
        self.stop = tk.Button(self.window, command = self.window.destroy, text="Quit").pack()

        self.window.mainloop()

    def popup(self):
        a = Second()
        self.one = a.get_one()

    def get_one(self):
        return self.one


if __name__ == '__main__':
     var = First()
     print(var.get_one())

#######################
#Test.py
import tkinter as tk

class Second:
    def __init__(self):
        self.popup = tk.Toplevel()
        myvar = tk.IntVar()
        self.one = tk.Radiobutton(self.popup, text="one", value = 1, variable = myvar, command = self.set_one)
        self.two = tk.Radiobutton(self.popup, text="two", value = 2, variable = myvar, command = self.set_two)
        self.done = tk.Button(self.popup, command = self.popup.destroy, text = "Bye")

        self.one.grid(row = 0, column = 0)
        self.two.grid(row = 0, column = 1)
        self.done.grid(row = 1, column = 0, columnspan = 2)

    def set_one(self):
        self.one = "Hi"

    def set_two(self):
        self.one = "Bye"

    def get_one(self):
        return self.one
moamstripes
  • 23
  • 1
  • 4
  • I think you might be able to use the `threading` module to run the other window in its own thread and use a `Queue` to pass information between it and the main (or another) thread. Here's an [example](https://stackoverflow.com/a/1835036/355230) of running a tkinter window in a separate thread. Using a `Queue` is fairly easy: just `put()` things in it, and `get()` them out whenever. – martineau Jun 05 '18 at 19:24
  • There's no need to use threads if you just need data from more than one window. – Bryan Oakley Jun 05 '18 at 21:28
  • Please show a [mcve]. `destroy` will only destroy the mainloop if you're destroying the root window. Destroying other windows shouldn't cause problems. – Bryan Oakley Jun 05 '18 at 21:29
  • @BryanOakley The code wouldn't fit here so here's a [pastebin](https://pastebin.com/ps5yZ9Xw) . If I don't use a "Second" class for the code, it works if I code the whole popup into the First class. When it's in the second, the code after `a = Second()` doesn't execute – moamstripes Jun 06 '18 at 00:08
  • Nobody wants a pastebin. You should be able to replicate your problem with just a dozen or two lines of code. You probably just need two windows and a couple of buttons. – Bryan Oakley Jun 06 '18 at 02:00
  • @BryanOakley My problem involves two files with imports and creating classes, and it exceeds the character limit in the comment section here but is about 40 lines. – moamstripes Jun 06 '18 at 16:31
  • You should never put code in the comment section. You can [edit] your question to add additional information. – Bryan Oakley Jun 06 '18 at 16:56
  • @bryanoakley Edited :) – moamstripes Jun 06 '18 at 18:08

0 Answers0