I have Program in Python Tkinter in which I have made three rows of 10 Labels and a Start Button Below them. I have used for loops to display text in the Labels.
First Row of Labels only Display Headings.
On Second row of Label I have set the condition that if loops value modulus equals zero (a%2==0)
it displays Labels text ON else OFF and this condition is opposite in third row of Label as shown below and in the code.
Finally on start button I have called a function that reveres the process that if loops variable value modulus 2 equals one then display labels text OFF on second row of Labels else ON. This process is opposite on third row of Labels. But the problem here is when I click on Start Button it only changes the last value of second and third row of Labels but I want to change all of them. I think the functions are not working properly as desired. Code is below.
from tkinter import *
import tkinter as tk
win = Tk()
win.title("Label")
win.geometry("800x600+50+50")
win.config(bg='white')
label1=Label(win, text="Label Status Changer", font=("Calibri",24,"bold"), bg='white', borderwidth=1, relief="solid", padx=20, pady=20) #"flat", "raised", "sunken", "ridge", "solid", and "groove"
label1.pack(pady=(15,60))
lblframe = tk.Frame(win)
for a1 in range(10):
pre1=Label(lblframe, text=("LBL",(a1+1)), font=("Calibri",12, "bold"), bg="white", borderwidth=1, relief="solid", padx=5, pady=2)
pre1.grid(row=0, column=a1)
for a2 in range(10):
if ( a2%2 == 0 ):
pre2=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
pre2.grid(row=1, column=a2, sticky="nw")
else:
pre2=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
pre2.grid(row=1, column=a2, sticky="nw")
for a3 in range(10):
if (a3%2 == 1):
pre3=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
pre3.grid(row=2, column=a3, sticky="nw")
else:
pre3=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
pre3.grid(row=2, column=a3, sticky="nw")
lblframe.pack()
def oldstatus():
for a4 in range(10):
if(a4%2==1):
pre2.config(text="OFF")
else:
pre2.config(text="ON")
def newstatus():
for a5 in range(10):
if(a5%2==0):
pre3.config(text="OFF")
else:
pre3.config(text="ON")
def statuschanger():
oldstatus()
newstatus()
#Button1
button1=Button(win,text="Start",width=10,height=2, font=("Calibri",16,"bold"), bg="black",fg="white", command=statuschanger)
button1.pack(pady=(30,0))
win.mainloop()
Output on Running Program
Output on Pressing Start Button
It works:
from tkinter import *
import tkinter as tk
win = Tk()
win.title("Label")
win.geometry("800x600+50+50")
win.config(bg='white')
label1=Label(win, text="Label Status Changer", font=("Calibri",24,"bold"), bg='white', borderwidth=1, relief="solid", padx=20, pady=20) #"flat", "raised", "sunken", "ridge", "solid", and "groove"
label1.pack(pady=(15,60))
list1=[]
list2=[]
lblframe = tk.Frame(win)
for a1 in range(10):
pre1=Label(lblframe, text=("LBL",(a1+1)), font=("Calibri",12, "bold"), bg="white", borderwidth=1, relief="solid", padx=5, pady=2)
pre1.grid(row=0, column=a1)
for l1 in range(10):
if l1%2 ==0:
list1.append(1)
else:
list1.append(0)
print(list1)
for l2 in range(10):
if l2%2 ==1:
list2.append(1)
else:
list2.append(0)
print(list2)
def mylabels():
for a2 in range(10):
if ( int(list1[a2])== 0 ):
pre2=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
pre2.grid(row=1, column=a2, sticky="nw")
#list1.append(pre2.cget("text"))
else:
pre2=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
pre2.grid(row=1, column=a2, sticky="nw")
#list1.append(pre2.cget("text"))
for a3 in range(10):
if (int(list2[a3])== 0):
pre3=Label(lblframe, text="OFF", font=("Calibri",12,"bold"), bg="white", fg="Green", borderwidth=1, relief="solid", padx=11, pady=1)
pre3.grid(row=2, column=a3, sticky="nw")
#list2.append(pre3.cget("text"))
else:
pre3=Label(lblframe, text="ON", font=("Calibri",12,"bold"), bg="white", fg="Red", borderwidth=1, relief="solid", padx=11, pady=1)
pre3.grid(row=2, column=a3, sticky="nw")
#list2.append(pre3.cget("text"))
lblframe.pack()
mylabels()
def statuschanger():
list1.clear()
list2.clear()
for l3 in range(10):
if l3%2 ==1:
list1.append(1)
else:
list1.append(0)
for l4 in range(10):
if l4%2 ==0:
list2.append(1)
else:
list2.append(0)
mylabels()
#Button1
button1=Button(win,text="Start",width=10,height=2, font=("Calibri",16,"bold"), bg="black",fg="white", command=statuschanger)
button1.pack(pady=(30,0))
win.mainloop()
Output on Running Programming
Output on Pressing start Button