I'm trying to create a button which has the same effect as the button from the version test from tkinter when using the command
py -m tkinter
in CMD. The button is supposed to display the text "Button", then with every press a set of brackets will be added on either side of the word. So on the first press, the text will read "[Button]", then the second press "[[Button]]", and so on.
Here is what I have:
from tkinter import *
from tkinter import ttk
def changeText():
textName = "[" + textName + "]"
root = Tk()
root.geometry('400x150')
root.title('Hit the button')
textName = "button"
frame = ttk.Frame(root, padding = 10).grid()
btn = ttk.Button(frame, text = textName, command = changeText).grid(column = 0, row = 0)
root.mainloop()
When I run my code, the window and button pops up correctly, however when the button is pressed, I get the error:
"X:\Pycharm Environments\venv\Scripts\python.exe" "X:/Pycharm Environments/Learning/tkinterPractie.py"
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\jared\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
return self.func(*args)
File "X:\Pycharm Environments\Learning\tkinterPractie.py", line 5, in changeText
textName = "[" + textName + "]"
UnboundLocalError: local variable 'textName' referenced before assignment
I'm not really sure what I'm doing wrong here. Thanks for any help!