0

This is a very simple version of my application. Basically I have a Tkinter application I want to start from another python application, but I do not have the option to have tkinter as my main window.

So my question is, if it's possible to pass some information from my main class to my tkinter application. Not once, but repeatedly.

I tried something like this, but this does not work, probably because of the .mainloop() method. Right now I just try to print the variable "labelContent" whenever the button is pressed.

Is there some way I can pass information to my tkinter application after calling .mainloop() (besides using external files like .txt because this would obviously work)?

gui.py

import tkinter 
class BasicWindow:
    def __init__(self):
        tk = tkinter.Tk()
        label = tkinter.Label(tk, text="Hello World!")
        label.pack()
        self.labelContent = ""
        button = tkinter.Button(tk,text="OK",command=self.buttonMethod)
        button.pack(side="bottom")
        tk.mainloop()
    
    def buttonMethod(self):
            print(self.labelContent)

main.py

from gui import BasicWindow
import time 

class Main:
    def __init__(self):
        self.gui = BasicWindow()
        self.gui.labelContent = "This is the new label content"

mainApp = Main()
while True:
    mainApp.gui.labelContent = time.time()

Thanks in advance! :)

Luca Tatas
  • 160
  • 1
  • 14
  • How are you running this script from the other program? popen? multiprocessing? Something else? – Bryan Oakley Dec 02 '20 at 15:45
  • For demonstration purposes I put both classes in this one file. Actually I have a main.py and gui.py where main.py contains the class Main and gui.py the class BasicWindow. So in my main.py I just do from gui import BasicWindow. – Luca Tatas Dec 02 '20 at 15:47
  • I edited my question. I hope I made myself more clear. – Luca Tatas Dec 02 '20 at 15:49
  • Why don't you just pass the string into `BasicWindow()` as a parameter and let it fill in `self.labelContent`? – quamrana Dec 02 '20 at 15:58
  • For this purpose this would work. But my problem is that in my actual application the self.labelContent is constantly updated, – Luca Tatas Dec 02 '20 at 15:59
  • How is that possible? There must be some other events going on to do that. – quamrana Dec 02 '20 at 16:01
  • I edited my code once again. Note that i am using time.time() only as an example. Actually I am trying to pass some actual data to my gui. My idea is that i want my app to be running all the time, but only open a gui which displays named data when the user requests this. – Luca Tatas Dec 02 '20 at 16:05
  • So, that doesn't work. Once your program gets to `tk.mainloop()`, its game-over for any other code like your last post. You should look into the `after()` method to give you some events to work with. – quamrana Dec 02 '20 at 16:08
  • hm okay, right now I am passing the information via a text document which seems to be extremely ugly, it works, but is not the way I'd like it to. – Luca Tatas Dec 02 '20 at 16:10
  • Hey, sorry but I deleted my answer, I hope you the best! You added as a comment that you want a tray application but "The stuff I need to do with data becomes so slow" if you use Tkinter directly. Honestly, I'm a little confused about what's going on here - hope someone comes along with a clearer picture! – en_Knight Dec 02 '20 at 16:19
  • This example won't work - your `while True` loop will not run until after the window is destroyed. We need a [mcve] that actually reproduces the conditions you're writing about. – Bryan Oakley Dec 02 '20 at 16:26

0 Answers0