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?
Answer
When you want to access the element of the list starting from the rightmost end, you can refer it using negative index values beginning from -1. Refer to sample examples mentioned below.
>>> li = [1,2,3,4]
>>> li[1]
2
>>> li[0]
1
>>> li[-1]
4
>>> li[-2]
3
No comments:
Post a Comment