函数来自模块调用函数,python

时间:2017-09-12 23:52:44

标签: python python-2.7 tkinter

好吧,我需要在more_example_funcs.py中获取该函数来更改页面。你可以在函数中看到我如何导入然后尝试运行正常的代码我会在example_gui.py(实际gui)里面。这是我用来简化问题的测试脚本,所以我可以学习如何修复它们。

more_example_funcs.py

from Tkinter import *
import Tkinter as tk
import example_gui as EG

def get_text(var):
    print var.get()
    return EG.self.controller.show_frame(Page_two) ## this will not run

example_gui.py

from Tkinter import *
import Tkinter as tk
# import example_funcs as EF
import more_example_funcs as MEF


class start(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        tk.Tk.title(self, "app name")
        menubar = tk.Menu(container)
        tk.Tk.config(self, menu=menubar)
        fileMenu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=fileMenu)
        fileMenu.add_command(label="Exit", command=quit)

        for F in (Page_one, Page_two, Page_three):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")


        self.show_frame(Page_one)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class Page_one(tk.Frame):

    def __init__(self, parent, controller, *args, **kwargs):
        self.controller = controller
        Frame.__init__(self, parent, *args, **kwargs)
        self.labels_one()
        self.buttons_one()
        self.entries_one()

    def labels_one(self):
        label1 = Label(self, text="Welcome to page one")
        label1.grid()

    def buttons_one(self):
        button_one = Button(self, text="go to page two", command=lambda:self.controller.show_frame(Page_two))
#        window_one_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 1"))
        text_one_button = Button(self, text="print entered text", command=lambda:MEF.get_text(self.entry_one_var))
        button_one.grid()
#        window_one_button.grid()
        text_one_button.grid()

    def entries_one(self):
        self.entry_one_var=StringVar()
        entry_one = Entry(self, textvariable= self.entry_one_var)
        entry_one.grid()

class Page_two(tk.Frame):

    def __init__(self, parent, controller, *args, **kwargs):
        self.controller = controller
        Frame.__init__(self, parent, *args, **kwargs)
        self.labels_two()
        self.buttons_two()

    def labels_two(self):
        label2 = Label(self, text="Welcome to page two")
        label2.grid()

    def buttons_two(self):
        button_two = Button(self, text="go to page three", command=lambda:self.controller.show_frame(Page_three))
#        window_two_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 2"))
        button_two.grid()
#        window_two_button.grid()

class Page_three(tk.Frame):

    def __init__(self, parent, controller, *args, **kwargs):
        self.controller = controller
        Frame.__init__(self, parent, *args, **kwargs)
        self.labels_three()
        self.buttons_three()

    def labels_three(self):
        label3 = Label(self, text="Welcome to page three")
        label3.grid()

    def buttons_three(self):
        button_three = Button(self, text="go to page one", command=lambda:self.controller.show_frame(Page_one))
#        window_three_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 3"))
        button_three.grid()
#        window_three_button.grid()

if __name__ == "__main__":        
    app = start()
#    EF.center(app)
    app.mainloop()

我试图让函数调用example_gui.py中的一个新函数,该函数调用页面进行更改,但我无法使导入功能正常。希望你们能帮助解决这个问题并解释它是如何运作的,这样我就可以从中学习。

2 个答案:

答案 0 :(得分:0)

您希望不惜一切代价避免循环导入。最好的解决方案是在课堂上将其作为一种方法。

class Page_one(tk.Frame):
    def buttons_one(self):
        text_one_button = Button(self, text="print entered text", command=self.get_text)
        button_one.grid()

    def get_text(self):
        print self.entry_one_var.get()
        self.controller.show_frame(Page_two)

否则,您将需要使用lambda调用导出函数以及变量。

您是否有理由在单独的文件中使用此功能?

答案 1 :(得分:0)

问题解决了。它确实是解决这个问题的唯一方法......

我将我的应用分为3页......

<强> example_gui_start.py

from Tkinter import *
import Tkinter as tk
import more_example_funcs as MEF

class start(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        tk.Tk.title(self, "app name")
        menubar = tk.Menu(container)
        tk.Tk.config(self, menu=menubar)
        fileMenu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=fileMenu)
        fileMenu.add_command(label="Exit", command=quit)

        for F in (MEF.EG.Page_one, MEF.EG.Page_two, MEF.EG.Page_three):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")


        self.show_frame(MEF.EG.Page_one)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

if __name__ == "__main__":
    app = start()
    MEF.EG.MEF.center(app)
    app.mainloop()

<强> more_example_funcs.py

## more_example_funcs.py
from Tkinter import *
import Tkinter as tk
import example_gui as EG
import __main__ as M

def center(toplevel):
    toplevel.update_idletasks()
    w = toplevel.winfo_screenwidth()
    h = toplevel.winfo_screenheight()
    size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
    x = w/2 - size[0]/2
    y = h/2 - size[1]/2
    toplevel.geometry("%dx%d+%d+%d" % (size + (x, y)))

def popupmsg(msg):
    popup = tk.Toplevel()
    popup.title("Information Dialog")
    label = Label(popup, text = msg)
    label.pack(side="top", pady=10)
    button = Button(popup, text = "OK", command = popup.destroy)
    button.pack()
    popup.grab_set()
    center(popup)
    popup.mainloop()


def get_text(self):
    print self.entry_one_var.get()
    M.app.show_frame(EG.Page_two)

<强> example_gui.py

from Tkinter import *
import Tkinter as tk
import more_example_funcs as MEF

class Page_one(tk.Frame):

    def __init__(self, parent, controller, *args, **kwargs):
        self.controller = controller
        Frame.__init__(self, parent, *args, **kwargs)
        self.labels_one()
        self.buttons_one()
        self.entries_one()

    def labels_one(self):
        label1 = Label(self, text="Welcome to page one")
        label1.grid()

    def buttons_one(self):
        button_one = Button(self, text="go to page two", command=lambda:self.controller.show_frame(Page_two))
        window_one_button = Button(self, text="open popup window", command=lambda:MEF.popupmsg("New window 1"))
        text_one_button = Button(self, text="print entered text", command=lambda:MEF.get_text(self))
        button_one.grid()
        window_one_button.grid()
        text_one_button.grid()

    def entries_one(self):
        self.entry_one_var=StringVar()
        entry_one = Entry(self, textvariable= self.entry_one_var)
        entry_one.grid()

class Page_two(tk.Frame):

    def __init__(self, parent, controller, *args, **kwargs):
        self.controller = controller
        Frame.__init__(self, parent, *args, **kwargs)
        self.labels_two()
        self.buttons_two()

    def labels_two(self):
        label2 = Label(self, text="Welcome to page two")
        label2.grid()

    def buttons_two(self):
        button_two = Button(self, text="go to page three", command=lambda:self.controller.show_frame(Page_three))
        window_two_button = Button(self, text="open popup window", command=lambda:MEF.popupmsg("New window 2"))
        button_two.grid()
        window_two_button.grid()

class Page_three(tk.Frame):

    def __init__(self, parent, controller, *args, **kwargs):
        self.controller = controller
        Frame.__init__(self, parent, *args, **kwargs)
        self.labels_three()
        self.buttons_three()

    def labels_three(self):
        label3 = Label(self, text="Welcome to page three")
        label3.grid()

    def buttons_three(self):
        button_three = Button(self, text="go to page one", command=lambda:self.controller.show_frame(Page_one))
        window_three_button = Button(self, text="open popup window", command=lambda:MEF.popupmsg("New window 3"))
        button_three.grid()
        window_three_button.grid()

如果你能注意到一些不同的东西。 1是我改变了命令= lambda:MEF.get_text(self),现在我可以使用我所有的“自我”。文件中的变量(example_gui.py)和我的函数(get_text)在文件中(more_example_funcs.py)...但这不是我的问题,只是让事情变得更容易,而且与此无关。

第二件事情发生了变化,最重要的是,内部文件(more_example_funcs.py)我正在“导入主要作为M”而不是“import example_gui_start”...他们是相同的文件,但它不会创建循环导入。在阅读了有关此问题的数小时后,Python Circular dependencies, unable to link variable to other file。这是我的赢家。

example_gui_start.py是我运行以使用我的GUI的文件。 example_gui.py是仅具有GUI外观代码的文件。 more_example_funcs.py是我用来放置与按钮/条目等有关的函数的文件。