python - 函数在Python Tkinter中不起作用吗?

标签 python python-3.x function tkinter label

我使用Python Tkinter编写了程序,在其中编写了三行10个标签,并在它们下面创建了一个开始按钮。我已使用循环在“标签”中显示文本。

标签的第一行仅显示标题。

Labels Headings

在Label的第二行中,我设置了以下条件:如果循环值模数等于零(a%2==0),则将Labels文本显示为ON,否则显示OFF,并且此条件在Label的第三行中与以下代码相反。

enter image description here

最终,在启动按钮上,我调用了一个函数,该函数验证以下过程:如果循环变量值模数2等于1,则在“标签”的第二行显示“标签”文本为“ OFF”,否则为“ ON”。此过程与“标签”的第三行相反。但是这里的问题是,当我单击“开始”按钮时,它仅更改了标签第二行和第三行的最后一个值,但我想更改所有标签。我认为这些功能无法正常工作。代码如下。

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()


运行程序输出

enter image description here

按下开始按钮时输出

enter image description here

最佳答案

有用:

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()


运行编程时输出

enter image description here

按下开始按钮时输出

enter image description here

关于python - 函数在Python Tkinter中不起作用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60313360/

相关文章:

python - 我该如何修复 AttributeError : 'dict_values' object has no attribute 'count' ?

python - Django错误: 'bool' object has no attribute '__getitem__'

python - 根据条件合并数据帧

python - 如何使用 Python 2.7 和 Python 3.4 启动两个 Spyder 实例?

python - 为什么 Python 是 3's PrettyPrinter behaving differently from Python 2' s,我如何获得相同的行为?

python - 如何使用 gpiozero button.when_pressed 函数来使用输入和输出整数的函数?

java - 将整数列表从 Java 传递到 Oracle 函数

python - 在 Flask 中从后台进程设置缓存

python - 如何解决这个问题?运行时错误: dictionary changed size during iteration

javascript - 仅当元素未获得焦点时才执行函数