摘要:单击tkinter数字调整框箭头会增加GUI中的数字,但对条目的检查与关联的StringVar()相比会因单击而滞后。在
我已经创建了包含数字项的复合小部件,当提交条目更改时,这些小部件调用模型计算。我不想每次更改条目时都直接调用模型,因为计算可能很慢。因此,实际代码使用<Tab>
、<Return>
等的绑定(在下面的示例代码中省略了),并且只在这样的“commit”上调用模型。在
我查看的SpinBox文档没有揭示绑定到SpinBox本身的向上/向下箭头按钮的方法。所以,我有点击和释放鼠标按钮的绑定。单击并释放时,将比较最后一个存储值与当前的StringVar值,如果它们不同,则更新存储值并调用模型。涉及的关键方法是bind_entry
、on_press
、on_release
、refresh
、entry_is_changed
和{on_press
和on_release
中有注释掉的代码,可以在按下数字调整框箭头时刷新模型;它被保留以使预期的最终行为清晰,但不需要复制错误)。在
from tkinter import * # technically bad practice, but common
class SpinBoxFrame(Frame):
"""
A tkinter Frame that holds a labeled entry widget with added behavior.
EntryFrame will call the function (provided as 'model' in the arguments)
when a change in the entry's value is committed.
Arguments (in addition to standard Frame options):
name-- for widget label and introspection
array-- a 2D array ( list of lists, [[],[]]
coord-- the coordinate of the array to be read from/written to
from_, to, increment: SpinBox arguments (minimum and maximum values,
and incremental change on each arrow click)
"""
def __init__(self, parent=None, name='', model=None,
array=None, coord=(0, 0),
from_=0.00, to=100.00, increment=1,
**options):
Frame.__init__(self, parent, **options)
self.name = name
self.model = model
self.array = array
self.row, self.col = coord
self.spinbox_kwargs = {'from_': from_,
'to': to,
'increment': increment}
self.initialize()
self.add_label()
self.add_entry()
self.bind_entry()
self.validate_entry()
def initialize(self):
self.value_var = StringVar()
self.value = self.array[self.row][self.col]
self.value_var.set(self.value)
def add_label(self):
Label(self, text=self.name, bg='white', bd=0).pack(side=TOP)
def add_entry(self):
self.entry = Spinbox(self, width=7,
validate='key', # check for number on keypress
**self.spinbox_kwargs)
self.entry.pack(side=TOP, fill=X)
self.entry.config(textvariable=self.value_var)
def bind_entry(self):
self.entry.bind('<FocusOut>', lambda event: self.refresh())
self.entry.bind('<ButtonPress-1>', lambda event: self.on_press())
self.entry.bind('<ButtonRelease-1>', lambda event: self.on_release())
def refresh(self):
if self.entry_is_changed():
print('VALUE CHANGE')
self.save_entry()
print('Saved new ', self.name, ' value')
self.model()
def entry_is_changed(self):
print('Old value of ', self.name, ' was ', self.value)
print('Current value of ', self.name, ' is ',
float(self.value_var.get()))
return self.value != float(self.value_var.get())
def save_entry(self):
if not self.value_var.get(): # if entry left blank,
self.value_var.set(0.00) # fill it with zero
self.value = float(self.value_var.get())
self.array[self.row][self.col] = self.value
def on_press(self):
print('Button pressed')
# self.loop_refresh() # uncomment to enable real-time refreshing
def loop_refresh(self):
self.refresh()
self.button_held_job = self._root().after(50, self.loop_refresh)
def on_release(self):
print('Button released')
# uncomment if loop enabled in on_press()
# self._root().after_cancel(self.button_held_job)
self.refresh()
def validate_entry(self):
"""
The base EntryFrame class assumes the entry contents should be numerical
"""
# check on each keypress if new result will be a number
self.entry['validatecommand'] = (self.register(self.is_number), '%P')
# sound 'bell' if bad keypress
self.entry['invalidcommand'] = 'bell'
@staticmethod
def is_number(entry):
"""
tests to see if entry is acceptable (either empty, or able to be
converted to a float.)
"""
if not entry:
return True # Empty string: OK if entire entry deleted
try:
float(entry)
return True
except ValueError:
return False
if __name__ == '__main__':
dummy_array = [[1, 42], [0, 0]]
root = Tk()
class TestFrame(Frame):
"""Mimics a toolbar object that holds entry widgets and passes their
entries on to a model"""
def __init__(self, parent, **options):
Frame.__init__(self, parent, **options)
def call_model(self):
print('requesting calculation from the model, using:')
print(dummy_array)
mainwindow = TestFrame(root)
mainwindow.pack()
box1 = SpinBoxFrame(mainwindow, array=dummy_array, coord=(0, 0),
name='Box 1', model=mainwindow.call_model)
box1.pack(side=LEFT)
box2 = SpinBoxFrame(mainwindow, array=dummy_array, coord=(0, 1),
name='Box 2', model=mainwindow.call_model)
box2.pack(side=LEFT)
# workaround fix for Tk problems and mac mouse/trackpad:
while True:
try:
root.mainloop()
break
except UnicodeDecodeError:
pass
单击并释放SpinBox箭头可以增加/减少GUI中的值,但与StringVar的比较表明没有变化。在同一方向上再次单击同一个箭头会导致更改,但更改为以前的值而不是当前的GUI值。因此,StringVar检查总是滞后于显示的值1。我想知道我的代码运行的速度和tkinter主循环更新与SpinBox条目关联的StringVar对象的速度是否有一些“竞争条件”。似乎调用entry_is_changed
的速度比更新StringVar的速度快。在
截图显示了bug的性质。首先,通过一次单击,左边的小部件从1增加到2,但是值检查仍然表明当前的StringVar仍然保持“1”。然后,正确的小部件被递增两次。第一次从42增加到43后,数值没有变化。第二次增量从43增加到44后,值43发生了变化。在
我认为
<ButtonRelease-1>
事件在变量值更新之前触发refresh
。为了避免这种情况,我在调用on_release
中的refresh
之前引入了一个小延迟:至少在我的电脑上,这个1毫秒的延迟足以得到更新的值。在
相关问题 更多 >
编程相关推荐