[英]Updating Tkinter label from a StringVar happens on a delay
I have a tkinter label object which is bound to a tkinter StringVar, which should in principle cause it to update when the StringVar is changed. 我有一个绑定到tkinter StringVar的tkinter标签对象,原则上应该在更改StringVar时使它更新。 Here is the declaration:
这是声明:
self.db_info_string = tk.StringVar()
self.db_info_string.set('Number of events: ' +str(len(self.eventsdb_subset)))
self.db_info_display = tk.Label(parent, textvariable=self.db_info_string)
Elsewhere in my GUI, I have a button, which when pressed calls this function: 在GUI的其他地方,我有一个按钮,按下该按钮时会调用此函数:
def filter_db(self):
filterstring = self.filter_entry.get()
print filterstring
eventsdb_subset = self.eventsdb_subset
self.eventsdb_subset = sqldf('SELECT * from eventsdb_subset WHERE %s' % filterstring,locals())
self.db_info_string.set('Number of events: ' +str(len(eventsdb_subset)))
(I'm aware of the problems using raw input in a DB like that - this is just for testing purposes at the moment, to get the hang of GUI programming, and will be sanitized before it sees the light of day) (我知道在这样的数据库中使用原始输入时会遇到的问题-目前这仅是出于测试目的,以摆脱GUI编程的束缚,并且在日趋成熟之前将被清除)
You can see that the value of the StringVar is changed in this function call. 您可以看到此函数调用中StringVar的值已更改。 However, the Label text is not updated until the second call to this function - ie, the label text lags the actual value it should be displaying by one call to this function.
但是,直到第二次调用此功能时,标签文本才更新-即,标签文本滞后于该功能的一次调用应显示的实际值。
Is there any way to force the Label to update in filter_db()? 有什么方法可以强制在filter_db()中更新标签?
eventsdb_subset = self.eventsdb_subset
self.eventsdb_subset = sqldf('SELECT * from eventsdb_subset WHERE %s' % filterstring,locals())
self.db_info_string.set('Number of events: ' +str(len(eventsdb_subset)))
eventsdb_subset
and self.eventsdb_subset
are independent variables. eventsdb_subset
和self.eventsdb_subset
是自变量。 Assigning to one won't change the value of the other one. 分配给一个不会改变另一个的值。
On the third line, eventsdb_subset
still has the value that it had before you ran self.eventsdb_subset = sqldf(...)
. 第三行,
eventsdb_subset
仍然具有运行self.eventsdb_subset = sqldf(...)
之前的值。 In other words, it's always going to lag one select
operation behind. 换句话说,它总是会落后一个
select
操作。
Update the StringVar using the value of self.eventsdb_subset
instead of eventsdb_subset
. 使用
self.eventsdb_subset
而不是eventsdb_subset
的值更新StringVar。
eventsdb_subset = self.eventsdb_subset
self.eventsdb_subset = sqldf('SELECT * from eventsdb_subset WHERE %s' % filterstring,locals())
self.db_info_string.set('Number of events: ' +str(len(self.eventsdb_subset)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.