python从另一个文件调用导航

问题描述 投票:0回答:1

如何从另一个python文件中发生的事件执行导航到第二页?这将是我的gui代码:

import tkinter as tk
from tkinter import *
import openf

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class Page1(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="first page")
       label.pack(side="top", fill="both", expand=True)

class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)


class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)


        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p1.show()


if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)
    root.resizable(0, 0)
    menubar = Menu(root)
    filemenu = Menu(menubar, tearoff=0)
    filemenu.add_command(label="Open", command=openf.openfiledialog)
    filemenu.add_command(label="Save", command=root.quit)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=root.quit)
    menubar.add_cascade(label="File", menu=filemenu)
    root.config(menu=menubar)
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("500x600")

我不需要使用按钮进行导航。我想在另一个py文件中成功执行函数时导航到第二页。我必须将main.py导入到另一个文件,但是如何从那里调用框架导航?

open F.朋友

from tkinter import filedialog

def openfiledialog():
    global of
    of = filedialog.askopenfilename(initialdir="/", title="Select file", filetypes=[("archives", "*.zip")])
    openfile())

def openfile():
    with zipfile.ZipFile(of, "r") as f:
    # navigate gui to second page from here
python tkinter navigation frame
1个回答
0
投票

似乎“导航到”其中一个页面的方法是在其上调用show方法。因此,您只需要对页面进行引用即可导航到该页面。

我建议在MainView上创建一个可用于导航到页面的方法。然后,您可以传入一个符号名称,它将使用该名称来确定要显示的页面。

例如:

class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.pages = {
            "p1": Page1(self),
            "p2": Page2(self),
        }
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        self.pages["p1"].place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        self.pages["p2"].place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        self.show("p1")

    def show(self, page_name):
        page = self.pages[page_name]
        page.show()

完成后,您只需将main传递给要导航到其他页面的函数即可。

例如,首先将main传递给另一个文件的openfiledialog方法:

...
filemenu.add_command(
    label="Open", 
    command=lambda: openf.openfiledialog(main)
)
...

然后在openfiledialog中,使用该引用来显示框架:

def openfiledialog(main):
    ...
    openfile(main)

def openfile(main):
    with zipfile.ZipFile(of, "r") as f:
        main.show("p1")
© www.soinside.com 2019 - 2024. All rights reserved.