我正在使用Tkinter为我的应用程序编写GUI。我正在使用类来描述整个结构,如here所述。由于某种原因,该程序在启动时运行与按钮绑定的命令(即使其中一个按钮被禁用),并且之后不会对相同的按钮作出反应。请参阅下面的代码。
from Tkinter import *
from new_plot_main import PlotClass
class MainWindow:
def __init__(self, master):
Frame(master).grid()
self.main_check = [NoneType, NoneType, NoneType, NoneType]
self.scope_check = [NoneType, NoneType, NoneType, NoneType]
self.main_check[0] = Checkbutton(master, text="Lines")
self.main_check[1] = Checkbutton(master, text="Errors")
self.main_check[2] = Checkbutton(master, text="Upstream Snr")
self.main_check[3] = Checkbutton(master, text="Downstream Snr")
self.scope_check[0] = Checkbutton(master, text="Lines")
self.scope_check[1] = Checkbutton(master, text="Errors")
self.scope_check[2] = Checkbutton(master, text="Upstream Snr")
self.scope_check[3] = Checkbutton(master, text="Downstream Snr")
self.stats_check = Checkbutton(master, text="Enable statistics") # Statistics trigger
self.csi_check = Checkbutton(master, text="Enable CSI") # CSI calculation trigger
self.scope_range = Entry(master, width=15).grid(row=4, column=3) # To specify scope range
self.lines_entry = Entry(master, width=15).grid(row=6, column=1) # Lines to analyze
self.save_to_pdf_button = Button(master,
text="Save to PDF",
state=DISABLED,
width=10)
self.plot_button = Button(master, text="Plot", width=10, command=self.plot_action())
self.set_button = Button(master,
text="Set", state=DISABLED,
width=10, command=self.scope_action()) # Button to apply range from scope_range
#------Setting coordinates via grid--------
Label(master, text="Main", font=("Helvetica", 10, "bold")).grid(row=0, column=0, sticky=SW)
Label(master, text="Scope", font=("Helvetica", 10, "bold")).grid(row=0, column=1, sticky=SW)
Label(master, text="Options", font=("Helvetica", 10, "bold")).grid(row=0, column=3, sticky=SW)
Label(master, text="Lines to plot").grid(row=6, column=0, sticky=E)
Label(master, text="Set scope").grid(row=3, column=3, sticky=S)
self.stats_check.grid(row=1, column=3, sticky=W)
self.stats_check.select()
self.csi_check.grid(row=2, column=3, sticky=W)
self.csi_check.select()
self.save_to_pdf_button.grid(row=6, column=4, sticky=E)
self.plot_button.grid(row=6, column=2, sticky=W)
self.set_button.grid(row=4, column=4, sticky=W)
for i in xrange(0, 4):
self.main_check[i].grid(row=i+1, column=0, sticky=W)
self.scope_check[i].grid(row=i+1, column=1, sticky=W)
self.main_check[i].select()
self.scope_check[i].select()
def plot_action(self):
# plot = PlotClass()
# self.top = Toplevel()
print "Potatoes"
def scope_action(self):
# self.bot = Toplevel()
print "Banana"
def handler():
"""
Exiting GUI and stopping all processes, when system exit button is pressed.
"""
root.quit()
root.destroy()
root = Tk()
root.wm_title("Plot tool [new]")
app = MainWindow(root)
root.protocol("WM_DELETE_WINDOW", handler)
root.mainloop()
此代码在执行时输出Potatoes
Banana
。
答案 0 :(得分:2)
您需要使用只是函数,而不是函数结果。而不是:
self.plot_button = Button(master, text="Plot", width=10,
command=self.plot_action())
和
self.set_button = Button(master, text="Set", state=DISABLED,
width=10, command=self.scope_action())
使用:
self.plot_button = Button(master, text="Plot", width=10,
command=self.plot_action)
和
self.set_button = Button(master, text="Set", state=DISABLED,
width=10, command=self.scope_action)
这样,只有在按下按钮时才会调用命令。