Reputation: 45
I have written the below code for displaying some content on GUI window. Some of the code I have removed from between which was not needed here. _print(text) function is used to print the content on the gui screen. Can someone tell me how I can make this function globally accessible so that _print statements in other functions of the same class also get updated on the GUI.
I cannot move this _print function outside of GUI() function.
Basically i need all my print statements to be pushed on tkinter. How do I do that?
def accept_client_connections(master=None):
try:
while True:
# some code
_print("This statement needs to be printed on tkinter")
def GUI():
***def _print(text):
tex.insert(END, text + '\n')
tex.see("end")***
window = Tk()
#some code
tex = Text(window,width=80, height=40, font=('arial',10,'bold'), wrap=WORD)
tex.pack(side=TOP, fill=X,padx=10,pady=5)
messagebox.showinfo("","Waiting")
_print('xvdgfd...')
if __name__ == "__main__":
Upvotes: 1
Views: 835
Reputation: 45
This might not be the best solution but this is how I figured out.
I have been able to solve this problem by making use of Classes. Both the functions were included in the class and using global variable under GUI() helped in making the tex variable globally accessible.
Upvotes: 1
Reputation: 316
You can do it like that:
def GUI():
def _print(text):
tex.insert(END, text + '\n')
tex.see("end")
return _print
# some code here...
_print = GUI()
_print("1234567890")
Upvotes: 0
Reputation: 66
I don't think you can do that, cause nested functions have the local scope and they are used as helper functions. instead, you can pass text argument to the outer function then use it within the inner function, here is how your code will look like.
def GUI(text):
def _print():
tex.insert(END, text + '\n')
tex.see("end")
_print()
# then call GUI function not _print
GUI('this is a text...')
window.mainloop()
Upvotes: 1