【发布时间】:2019-10-22 20:08:47
【问题描述】:
我正在尝试创建一个脚本来创建一个 GUI 类的实例,它本身具有子类来表示我想要显示的各种页面。
目标是创建一个调用特定 GUI 页面来显示的序列文件,一旦显示该 GUI 页面并单击按钮,它就会运行序列文件中的序列,然后指示要显示哪些 GUI 页面.
Seqeuncer 看起来像这样(显示测试页的标准已简化)
import gui
class Sequencer():
def __init__(self):
pass
def run_gui(self):
# Create instance of the Test GUI
self.my_gui = gui.TestGUI()
self.my_gui.mainloop()
def run_test(self, entry):
if x = 1:
gui.TestGUI().show_page(PageOne)
if x = 2:
gui.TestGUI().show_page(PageTwo)
图形用户界面如下:
import sequencer
class TestGUI(tk.Tk):
def __init__(self):
# Initialize the container
tk.Tk.__init__(self)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
# Make the container a fixed size
tk.Tk.resizable(self, False, False)
# Set up the pages for the GUI
self.pages = {}
list_of_pages = [
PageOne,
PageTwo
]
for P in list_of_pages:
frame = P(container, self)
self.pages[P] = frame
frame.grid(row=0, column=0, sticky="nsew")
# Set the Main Page
self.show_page(MainPage)
def show_page(self, page):
frame = self.pages[page]
frame.tkraise()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
entry = tk.Entry(self, width=40, font="Arial 16", justify="center")
entry.place(relx=0.5, rely=0.5, anchor="center")
# Create Binding for the Return Key
entry.bind("<Return>", lambda event: sequencer.Sequencer().run_test(entry.get()))
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
然后我创建一个顶级脚本 (main.py),它创建一个 Sequencer 实例并运行 gui。我没有显示应该显示的页面,而是出现错误。
我做错了吗?
【问题讨论】:
-
“我做错了吗?”:你没有显示Full Traceback。 edit你的问题相应地。
标签: python python-2.7 tkinter