This is the part of the code where im having trouble with:
#file1:
from tkinter import *
import file2
chat = Text()
def createGUI():
main = Tk()
main.title("main")
main.geometry("1280x720")
main.resizable()
global chat
chat = Text(main)
chat.place(x=775, y=20, height=575, width=450)
file2.StartThread()
mainloop()
def InsertMSG(message):
global chat
chat.insert(1.0, message)
if __name__ == '__main__':
CreateGUI()
In the other file, this is the code:
#file 2:
import file1
import threading
def StartThread():
listening = threading.Thread(target=listen)
listening.start()
def listen():
while True:
message = input("Enter your message")
file1.InsertMSG(message)
Quick note, in the actual code the message that's supposed to be printed in the Text widget is being received from a client and transpored throught a socket, but this part works perfectly so it's irelevant to the problem.
I'm pretty certain that the Text isn't displayed due to the fact that I'm trying to insert the text after the mainloop() statement, but I have no idea how to fix it. (I've saw some solutions using tk.update_idletasks() and tk.update() but I couldn't seem to make it work.
Thanks in advance!
When you start
file1then it createsText(main)but whenfile2importsfile1then it doesn't get thischatbut it creates newchat = Text()which is not displayed in window. So later it sends text to Text which you can't see.You should send
chatas parameter to function infile2and it should send it as parameter to
listenerand it should send it as parameter to
InsertMSGand it should get it
This way it will use the same Text as it displays in window.
file1.py
file2.py
PEP 8 -- Style Guide for Python Code
But I would rather use
tkinter.Entry()to get text - and it wouldn't need thread.