使用命令缩放小部件和按钮,在缩放值更改之前阻止命令
Scale widget and button with command, prevent command until scale value changes
我有一个 GUI,其中包含一个刻度和一个按钮小部件。单击该按钮时,它会调用一个函数来删除比例小部件。我希望只有当比例值发生变化时才会发生这种情况,也就是说,我希望人们在激活按钮命令之前移动比例光标。比例尺的默认值为 0,人们可以移动光标然后回到 0。我已经尝试了很多东西,但无法弄清楚如何以简单的方式做到这一点。
提前致谢!
这是我的代码的简化版本:
from tkinter import *
def action(widget):
widget.destroy()
window = Tk()
value = DoubleVar()
scale = Scale(window, variable=value, resolution=1)
button = Button(window, command = lambda: action(scale))
scale.pack()
button.pack()
window.mainloop()
这是一个使用 .trace
方法的新版本,正如@Sun Bear 所建议的。还是不行,"action"函数好像没有得到更新后的状态变量。
from tkinter import *
def scalestate(*arg):
scale_activate = True
print("scale_activate is", scale_activate)
def action(widget):
if scale_activate:
widget.destroy()
window = Tk()
scale_activate = False
print("scale_activate is initially", scale_activate)
value = DoubleVar()
value.trace('w', scalestate)
scale = Scale(window, variable=value, orient=HORIZONTAL)
button = Button(window, command = lambda: action(scale))
scale.pack()
button.pack()
window.mainloop()
您可以在销毁前比较这些值。并且 DoubleVar 对 Scales 没有用。由于 Scales 有一个 get() 函数
from tkinter import *
def action(widget):
If widget.get() != original:
widget.destroy()
window = Tk()
scale = Scale(window, resolution=1)
original = scale.get()
button = Button(window, command = lambda: action(scale))
scale.pack()
button.pack()
window.mainloop()
您可以使用.trace
方法来监控刻度值,而不是切换状态变量。此后,方法 "action" 可用于在决定是否删除比例小部件之前检查状态变量和比例值。
下面的代码根据您的规范执行。如果您认为条件过于详细,您可以探索最小化条件粒度的方法。
请注意,我更改了您的代码以更加面向对象的方式来表达它。它使实现你想要的更容易。这也是一种非常有用的方法,因为您以后需要做更复杂的事情。
import tkinter as tk
class SampleApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.scale_activate = False
self.value = tk.DoubleVar()
self.value.trace('w', self.scalestate) #add a trace to the scale variable
self.scale = tk.Scale(parent, variable=self.value, resolution=1)
self.button = tk.Button(parent,
command=lambda:self.action(self.scale))
self.scale.pack()
self.button.pack()
def scalestate(self, *arg):
# method to toggle scale has been activated
if self.value.get():
print('Tracing Scale value: ', self.value.get())
self.scale_activate=True
def action(self, widget):
# method to delete scale widget according to your stated conditions
if self.value.get():
if self.scale_activate:
print('self.value>0, self.scale_activate is True, delete scale')
widget.destroy()
self.scale_activate = False
self.value.set(0.0)
else:
print('self.value>0, self.scale_activate is False')
self.scale_activate = True
else:
if self.scale_activate:
print('self.value==0, self.scale_activate is True, delete scale')
widget.destroy()
self.scale_activate = False
self.value.set(0.0)
else:
print('self.value==0, self.scale_activate is False')
self.scale_activate = False
if __name__ == "__main__":
window = tk.Tk()
app = SampleApp(window)
window.mainloop()
我有一个 GUI,其中包含一个刻度和一个按钮小部件。单击该按钮时,它会调用一个函数来删除比例小部件。我希望只有当比例值发生变化时才会发生这种情况,也就是说,我希望人们在激活按钮命令之前移动比例光标。比例尺的默认值为 0,人们可以移动光标然后回到 0。我已经尝试了很多东西,但无法弄清楚如何以简单的方式做到这一点。
提前致谢!
这是我的代码的简化版本:
from tkinter import *
def action(widget):
widget.destroy()
window = Tk()
value = DoubleVar()
scale = Scale(window, variable=value, resolution=1)
button = Button(window, command = lambda: action(scale))
scale.pack()
button.pack()
window.mainloop()
这是一个使用 .trace
方法的新版本,正如@Sun Bear 所建议的。还是不行,"action"函数好像没有得到更新后的状态变量。
from tkinter import *
def scalestate(*arg):
scale_activate = True
print("scale_activate is", scale_activate)
def action(widget):
if scale_activate:
widget.destroy()
window = Tk()
scale_activate = False
print("scale_activate is initially", scale_activate)
value = DoubleVar()
value.trace('w', scalestate)
scale = Scale(window, variable=value, orient=HORIZONTAL)
button = Button(window, command = lambda: action(scale))
scale.pack()
button.pack()
window.mainloop()
您可以在销毁前比较这些值。并且 DoubleVar 对 Scales 没有用。由于 Scales 有一个 get() 函数
from tkinter import *
def action(widget):
If widget.get() != original:
widget.destroy()
window = Tk()
scale = Scale(window, resolution=1)
original = scale.get()
button = Button(window, command = lambda: action(scale))
scale.pack()
button.pack()
window.mainloop()
您可以使用.trace
方法来监控刻度值,而不是切换状态变量。此后,方法 "action" 可用于在决定是否删除比例小部件之前检查状态变量和比例值。
下面的代码根据您的规范执行。如果您认为条件过于详细,您可以探索最小化条件粒度的方法。
请注意,我更改了您的代码以更加面向对象的方式来表达它。它使实现你想要的更容易。这也是一种非常有用的方法,因为您以后需要做更复杂的事情。
import tkinter as tk
class SampleApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.scale_activate = False
self.value = tk.DoubleVar()
self.value.trace('w', self.scalestate) #add a trace to the scale variable
self.scale = tk.Scale(parent, variable=self.value, resolution=1)
self.button = tk.Button(parent,
command=lambda:self.action(self.scale))
self.scale.pack()
self.button.pack()
def scalestate(self, *arg):
# method to toggle scale has been activated
if self.value.get():
print('Tracing Scale value: ', self.value.get())
self.scale_activate=True
def action(self, widget):
# method to delete scale widget according to your stated conditions
if self.value.get():
if self.scale_activate:
print('self.value>0, self.scale_activate is True, delete scale')
widget.destroy()
self.scale_activate = False
self.value.set(0.0)
else:
print('self.value>0, self.scale_activate is False')
self.scale_activate = True
else:
if self.scale_activate:
print('self.value==0, self.scale_activate is True, delete scale')
widget.destroy()
self.scale_activate = False
self.value.set(0.0)
else:
print('self.value==0, self.scale_activate is False')
self.scale_activate = False
if __name__ == "__main__":
window = tk.Tk()
app = SampleApp(window)
window.mainloop()