Home > OS >  How to send messages to a customTkinter textbox from a different script
How to send messages to a customTkinter textbox from a different script

Time:12-13

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?

CodePudding user response:

You should define the status_update function within whichever script (the "separate script" you mentioned) you main Python file is calling, then import that function into the main file. Always import from the 'bottom up' - your "sub-files" (for lack of a better word) should never call anything defined in your main file.

# do this in 'main.py' (or whatever it's called)
from other_file import status_update

CodePudding user response:

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")
  • Related