Search code examples
pythonpython-3.xtkintertkinter-text

How to send messages to a customTkinter textbox from a different script


This might be a very newbie question but I'm struggling to find a solution.

I have a main Python script with a customTkinter window that has a textbox were I post all of my message updates defined:

def status_update(self, message):
    self.status_textbox.configure(state="normal")
    self.status_textbox.insert("1.0", message)
    self.status_textbox.configure(state="disabled")

Inside my class "class App(customtkinter.CTk):"

The question is, how can I use that status_update function from a separate script that I'm executing inside the cTk class?


Solution

  • I would give the script, e.g. to a class there, an instance of your app. Use the keyword self inside App.

    random_script.py:

    class RandomScript:
    
       def __init__(self, app):
           self.app = app
    
       def say_hi(self, name):
           text = "Hi " + name
           self.app.stauts_update(text)
    

    Somewhere in app.py:

    from random_script import RandomScript
    
    ...
    
    random_script = RandomScript(self)
    random_script.say_hi("Pete")