【发布时间】:2017-05-18 16:34:24
【问题描述】:
我已经阅读了许多关于 update_idletasks() 的帖子。我以为我理解了,我将其添加到代码中。标签应该显示 # 3 因为我只有 3 行记录,但它没有。
我确实使用了一个按钮,并且标签有效,但 update_idletask() 没有。
有没有办法不使用按钮通过 update_idletasks() 使这个标签自我更新?
我认为它不起作用是因为它没有进入循环? 请指导我。
import sqlite3
import tkinter as t
import time
class SampleApp(t.Tk):
def __init__(self,parent):
t.Tk.__init__(self,parent)
self.parent=parent
self.initialize()
def initialize(self):
self.grid()
#GUI-------------------------------------
self.labelVariable=t.StringVar()
self.label=t.Label(self, textvariable=self.labelVariable, bg="yellow", width=50, height=20)
self.label.pack(fill=t.X, expand=1)
#self.button=t.Button(self, text="get status", pady=5, command=self.status)
#self.button.pack(anchor=t.S, fill=t.X, expand=1)
#formula______________________________________
def status(self):
conn=sqlite3.connect("employees.db")
cursor=conn.cursor()
cursor.execute("SELECT COUNT(*) FROM Company")
self.labelVariable.set(cursor.fetchone()[0])
self.update_idletasks()
self.after(0,self.status)
app = SampleApp(None)
app.mainloop()
【问题讨论】: