简体   繁体   English

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

[英]Functions are not Working in Python Tkinter?

I have Program in Python Tkinter in which I have made three rows of 10 Labels and a Start Button Below them.我使用Python Tkinter编写了程序,在其中编写了三行10个标签,并在它们下面创建了一个开始按钮。 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.在Label的第二行上,我设置了以下条件:如果循环值模数等于零(a%2==0)则它将Labels文本显示为ON,否则显示OFF,并且此条件在Label的第三行中与下面的代码相反。

在此处输入图片说明

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.最后,在启动按钮上,我调用了一个函数,该函数验证以下过程:如果循环变量值模数2等于1,则在“标签”的第二行上显示“标签”文本为“ OFF”,否则显示为“ 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按下开始按钮时输出

在此处输入图片说明

I have Program in Python Tkinter in which I have made three rows of 10 Labels and a Start Button Below them.我使用Python Tkinter编写了程序,在其中编写了三行10个标签,并在它们下面创建了一个开始按钮。 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.在Label的第二行上,我设置了以下条件:如果循环值模数等于零(a%2==0)则它将Labels文本显示为ON,否则显示OFF,并且此条件在Label的第三行中与下面的代码相反。

在此处输入图片说明

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.最后,在启动按钮上,我调用了一个函数,该函数验证以下过程:如果循环变量值模数2等于1,则在“标签”的第二行上显示“标签”文本为“ OFF”,否则显示为“ 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按下开始按钮时输出

在此处输入图片说明

I have Program in Python Tkinter in which I have made three rows of 10 Labels and a Start Button Below them.我使用Python Tkinter编写了程序,在其中编写了三行10个标签,并在它们下面创建了一个开始按钮。 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.在Label的第二行上,我设置了以下条件:如果循环值模数等于零(a%2==0)则它将Labels文本显示为ON,否则显示OFF,并且此条件在Label的第三行中与下面的代码相反。

在此处输入图片说明

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.最后,在启动按钮上,我调用了一个函数,该函数验证以下过程:如果循环变量值模数2等于1,则在“标签”的第二行上显示“标签”文本为“ OFF”,否则显示为“ 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按下开始按钮时输出

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM