[英]Command in the button executes without showing the button widget.(Python tkinter)
[英]Function executes correctly as a tkinter button command but not in the script
这是我关于 StackOverflow 的第一个问题。 到目前为止,潜伏已经足以解决我所有的问题。
我是一个 python 新手,我还没有完全理解“自我”背后的含义。
我定义了一个函数(不是方法。它不在类内)作为
def pcal_thresh(self):
p_th = p_thresh.get()
print('p_th')
我试图在 2 个不同的条件下使用它。 首先作为 Tkinter 的命令
p_thresh = tk.Scale(calibration, from_=255, to=1, length=int(y_height*1.2), command=pcal_thresh)
二、在另一个函数里面
def confirm():
if not top_distance == 0:
pcal_thresh()
在这个确切的配置中,函数“pcal_thresh()”作为 Tkinter 命令正确执行,但不在另一个函数中。 如果我从声明中删除“自我”,则相反。 在函数内部使用时可以正常工作,但不能作为 Tkinter 命令使用。 这里有什么问题?
self
没有默认值,因此即使您不使用它,您仍然需要在调用pcal_thresh
时提供一个值。 作为回调,它在调用时接收新的比例值。
要么提供一个虚拟参数
def confirm():
if not top_distance == 0:
pcal_thresh(None)
或提供一个默认值:
def pcal_thresh(self=None):
p_th = p_thresh.get()
print('p_th')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.