更新python3 tkinter标签不起作用

时间:2018-03-05 09:18:02

标签: python-3.x tkinter

如果我点击手动按钮,我可以手动更新标签和条目,但如果我点击了自动按钮......控制台显示随机数,但小部件没有更新。

from tkinter import *
import random
import time

def manual_settxt():
    for t in range(0,3):
        rd = random.randrange(1,100)
        labelWidgets[t].configure(text=rd)
        entryWidgets[t].delete(0,END)
        entryWidgets[t].insert(0,rd)

def auto_settxt():
    while True:
        time.sleep(3)
        for t in range(0,3):
            rd = random.randrange(1,100)
            print(rd)
            labelWidgets[t].configure(text=rd)
            entryWidgets[t].delete(0,END)
            entryWidgets[t].insert(0,rd)

root = Tk()

namesInput = [1,2,3]
entryWidgets = []
labelWidgets = []

for i in range(0, len(namesInput)):
    labelWidgets.append(Label(root,text=namesInput[i],justify='center'))
    entryWidgets.append(Entry(root,width=5,justify='center'))
    labelWidgets[-1].grid(row=i+1,column=0)
    entryWidgets[-1].grid(row=i+1,column=1)

b1 = Button(root, text = "Manual", command=manual_settxt)
b1.grid(row=4,column=0)

b2 = Button(root, text = "Auto", command=auto_settxt)
b2.grid(row=4,column=1)

root.mainloop()

2 个答案:

答案 0 :(得分:1)

您创建了一个无限循环。删除while True。 我重构了一些代码:使用Thread进行非阻塞功能。 试试这个

from tkinter import *
import random
import time
from threading import Thread

def manual_settxt():
    for index in range(3):
        rd = random.randrange(1,100)
        labelWidgets[index].configure(text=rd)
        entryWidgets[index].delete(0,END)
        entryWidgets[index].insert(0,rd)

def job():
    for index in range(3):
        time.sleep(1)
        rd = random.randrange(1,100)
        print(rd)
        labelWidgets[index].configure(text=rd)
        entryWidgets[index].delete(0,END)
        entryWidgets[index].insert(0,rd)


def auto_settxt():
    Thread(target=job).start()

root = Tk()

namesInput = [1,2,3]
entryWidgets = []
labelWidgets = []

for index, name in enumerate(namesInput):

    labelWidgets.append(Label(root, text=name, justify='center'))
    entryWidgets.append(Entry(root, width=5, justify='center'))

    labelWidgets[-1].grid(row=index+1,column=0)
    entryWidgets[-1].grid(row=index+1,column=1)

b1 = Button(root, text = "Manual", command=manual_settxt)
b1.grid(row=4,column=0)

b2 = Button(root, text = "Auto", command=auto_settxt)
b2.grid(row=4,column=1)

root.mainloop()

答案 1 :(得分:0)

我使用“root.after”并且脚本似乎工作正常。

from tkinter import *
import random
import time

def manual_settxt():
    for index in range(3):
        rd = random.randrange(1,100)
        labelWidgets[index].configure(text=rd)
        entryWidgets[index].delete(0,END)
        entryWidgets[index].insert(0,rd)

def auto_settxt():
        for index in range(3):
            rd = random.randrange(1,100)
            print(rd)
            labelWidgets[index].configure(text=rd)
            entryWidgets[index].delete(0,END)
            entryWidgets[index].insert(0,rd)
        root.after(1000, auto_settxt)

root = Tk()

namesInput = [1,2,3]
entryWidgets = []
labelWidgets = []

for index, name in enumerate(namesInput):

    labelWidgets.append(Label(root, text=name, justify='center'))
    entryWidgets.append(Entry(root, width=5, justify='center'))

    labelWidgets[-1].grid(row=index+1,column=0)
    entryWidgets[-1].grid(row=index+1,column=1)

b1 = Button(root, text = "Manual", command=manual_settxt)
b1.grid(row=4,column=0)

b2 = Button(root, text = "Auto", command=auto_settxt)
b2.grid(row=4,column=1)

root.after(1000, auto_settxt)
root.mainloop()