Here's the code I have confusion with
from tkinter import *
root = Tk()
root.geometry("100x200")
def changeOne():
if B.config('text')[-1] == "ITE 003":
B.config(text="Hello", fg="red")
else:
B.config(text="ITE 003", fg ="green")
def changeTwo():
if label.config('text')[-1] == "Green Text":
B2.config(fg ="red")
label.config(text ="Red Text", fg = "red")
else:
B2.config(fg ="green")
label.config(text ="Green Text", fg = "green")
B = Button(root, text = "Hello", fg="red", command=changeOne)
B2 = Button(root, text = "Click Me", fg="red", command=changeTwo)
label = Label(root, text ="Red Text", fg = "red")
B.place(x = 10,y = 10)
B2.place(x = 10, y = 40)
label.place(x = 10, y = 70)
root.mainloop()
The problem is with this part of the if condition:
if B.config('text')[-1] == "ITE 003":
, the [-1]
. I really don't understand how the [-1]
helps retrieve the actual button's text value, why does it need the [-1]
? Is it like an array thing?