当Python处理这两行时,它会在第一行看到这一点:
os.startfile('Bessel.pdf')并将其解释为有效的函数调用。因此,它调用了该函数。
要解决此问题,请定义一个函数来处理该行之前的click事件,然后将按钮的
commandopion分配给该函数:
def handler(): # The pre in here is "hidden" # It will only run when the function is called (the button is clicked) os.startfile('Bessel.pdf')self.B3 = Button(Inputframe, text='Graphical Plots', command=handler, bd=5, width=13, font="14")或者,在这种情况下,甚至更好/更干净的方法是使用
lambda(匿名函数):
self.B3 = Button(Inputframe, text='Graphical Plots', command=lambda: os.startfile('Bessel.pdf'), bd=5, width=13, font="14")或者,就像@JFSebastian指出的那样,您可以使用
functools.partial:
self.B3 = Button(Inputframe, text='Graphical Plots', command=functools.partial(os.startfile, "Bessel.pdf"), bd=5, width=13, font="14")
请注意,您必须先导入
functools。



