I have a tkinter.Label created inside a function and from a totally seperate part of my code I need to update the text.
I have tried just about every solution google provides over the last hour and I can't get any of them to work, some error, some show blanks, some just fail to do anything.
I am creating the labels as follows
def createWindow():
window = tkinter.Tk()
container = tkinter.Frame(window, padx=5, pady=5)
summaryFrame = tkinter.Frame(container, bd=2, relief='groove')
summaryFrame.pack(side='top', fill='x')
summaryUser = tkinter.Label(summaryFrame, text='Some text').grid(row=1, column=1, sticky='w')
Much later I need to change the text of this label but because I'm no longer in this createWindow() function I don't have access to the summaryUser variable that contains the text.
I have tried summaryEvent["text"] (errors because it's not available), I have tried using a global variable and using textvariable=AGlobalVariable instead of text='Some text' (leaves the label text blank) and many other google results all with no success.
This seems like the sort of functionality that should be easier than this...
EDIT 1
I have tried the following...
summaryUserText = 'Some text'
def createWindow():
global summaryUserText
window = tkinter.Tk()
container = tkinter.Frame(window, padx=5, pady=5)
summaryFrame = tkinter.Frame(container, bd=2, relief='groove')
summaryFrame.pack(side='top', fill='x')
summaryUser = tkinter.Label(summaryFrame, textvariable=summaryUserText)
summaryUser.grid(row=1, column=1, sticky='w')
When I try this the label just starts blank, not with the content of the variable.
EDIT 2
I have also tried the following...
summaryUserText= tkinter.StringVar()
summaryUserText.set('Some text')
def createWindow():
...
summaryUser= tkinter.Label(summaryFrame, textvariable=summaryUserText)
But as soon as python sees the first line it errors with the following...
File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 480, in __init__
Variable.__init__(self, master, value, name)
File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 317, in __init__
self._root = master._root()
AttributeError: 'NoneType' object has no attribute '_root'
Edit 3
The simplest code that simulates the issue in one complete file
import tkinter
def loadEvent():
global summaryEventText
summaryEventText.set('Updated')
print('Updated')
def createWindow():
global summaryEventText
window = tkinter.Tk()
summaryEventText = tkinter.StringVar()
summaryEventText.set('Init')
summaryEventLabel = tkinter.Label(window, text='Event:').grid(row=0, column=0, sticky='e')
summaryEvent = tkinter.Label(window, textvariable=summaryEventText).grid(row=0, column=1, sticky='w')
window.mainloop()
createWindow()
loadEvent()
No errors, the print('Updated') works but the summaryEventText.set('Updated') does nothing.