如何切换到主框架

时间:2017-06-29 16:58:38

标签: python-3.x user-interface tkinter tkinter-canvas

当用户按下Done按钮时,我正在尝试返回主页面。

我有两个文件:

gui_main.py:有几个框架的主要gui

gui_scan.py:当用户按下gui_main.py按钮时,执行扫描并返回位于Done的主菜单框。

问题:每当我从gui_main.py调用一个帧时,我都会得到一个KeyError,因为该帧不在main的frames列表中。 经过大量调试后,我发现我在gui_main.py中错误地引用了这个框架。这就是我引用它的方式:

这是一个MVCE等效代码:

#gui_main.py
import tkinter as tk 
from tkinter import ttk 

# External file imports
import gui_scan as scan
from gui_frame import PageFrame


class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        print("Loading")

        print ("Constructing Frames")
        self.frames = {}
        for F in (PageOne,Menu, Settings, scan.PerformScan):
            frame = F(container, self)
            self.frames[F] = frame 

    def show_frame(self, frame_name):
        """ Show @frame_name frame """ 
        self.active_frame = self.frames[frame_name]  # <-- this causes the error
        self.active_frame.tkraise()

# PageOne Frame Class
class PageOne(PageFrame):
    def __init__(self, parent, controller):
    """ ...."""
    # Implementation of this is iirelavant to question

# Menu Frame Class  
class Menu(PageFrame):
    def __init__(self, parent, controller):
    """ ...."""
    # Implementation of this is iirelavant to question

# Settings Frame Class
class Settings(PageFrame):
    def __init__(self, parent, controller):
    """ ...."""
    # Implementation of this is iirelavant to question

if __name__ =="__main__":
    try:
        app = MainWindow()
    except Exception:
        # Problems
        print ("Problems running GUI")

另一个档案:

#gui_scan.py
import tkinter as tk 

from gui_frame import PageFrame
import gui_main as main 

class PerformScan(PageFrame):
    def __init__(self, parent, controller):
        PageFrame.__init__(self, parent, controller)
        label = tk.Label(self, padx=0, text="When you are ready press \"Start\" \n", justify=tk.CENTER)
        label.pack()

        button = tk.Button(self, text='Start', command=lambda: self.run_scan())
        button.pack()

    def run_scan(self):
        """ Run a scan"""
        # The scan runs here

        # at the end, have the user press `Done` and go back to Menu from `gui_main.py`
        self.button_frame = tk.Frame(self.final_frame, bg="red")
        self.button = tk.Button(self.button_frame, text = "Done",
            command = lambda:self.controller.show_frame(main.Menu)) # <-- this is wrong,(I think)
        self.button.grid(row = 0, column=3, padx = 5)
        self.button_frame.pack()

调试: 从main_gui中打印出self.frames中的帧:

Frame:  <class '__main__.PageOne'>
Frame:  <class '__main__.Menu'>
Frame:  <class '__main__.Settings'>
Frame:  <class 'gui_scan.PerformScan'>

Done中的run_scan(self)按钮即可:

<class 'gui_main.Menu'>

这会产生错误,我会陷入任何帧状态而不是切换回Menu帧。

1 个答案:

答案 0 :(得分:0)

我明白了。

解决方案:

从主文件中分离class Menu(PageFrame)class Settings(PageFrame)gui_scan.py,然后将其导入function myFunction() { "comment" //Or comment alert("hello"); }。这避免了尝试导入主文件的问题,也使主文件更有条理。