使用带有按钮-Tkinter的模块的功能

时间:2017-09-12 00:11:47

标签: python python-2.7 tkinter

所以我正在学习模块。使用它5个月左右后,我对tkinter很体面。如果我把我的功能放在我的主文件中,我可以让它工作。我将它们分成单独的模块,这样我就可以学习如何更好地使用模块。所以这个问题更多的是知识。

我将共有3个文件,我的主循环(example_gui.py),我的pythonic函数(example_funcs.py),我的GUI函数(more_example_funcs.py)...你可以看到我的问题是使用“get_text()”里面的more_example_funcs.py很明显,为什么它在这种情况下不起作用。该.py文件中未定义该变量。我怎么做这个工作?我被告知通过将函数放在另一个文件(模块)中来编写代码是更好的方法。

使用Tkinter的全尺寸应用程序,我将有一堆连接到条目的函数,这些函数将在example_gui.py中定义,如果我可以将函数放在more_example_funcs中,那将会容易得多.py就像我下面的例子那样

example_funcs.py

from Tkinter import *
import Tkinter as tk

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()

more_example_funcs.py

from Tkinter import *
import Tkinter as tk

def get_text():
    print entry_one_var.get()

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=MEF.get_text)
        button_one.grid()
        window_one_button.grid()
        text_one_button.grid()

    def entries_one(self):
        entry_one_var=StringVar()
        entry_one = Entry(self, textvariable= 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()

app = start()
EF.center(app)
app.mainloop()

1 个答案:

答案 0 :(得分:0)

使get_text函数获取参数,以便稍后可以在任何变量上调用它。

more_example_funcs.py

from Tkinter import *
import Tkinter as tk

def get_text(var):
    print var.get()

另外,使用entry_one_var关键字(Page_one)在self中使self.entry_one_var成为一个类变量,因为您需要使用多种方法,然后当您致电self.entry_one_var时,请将get_text作为参数传递。

这就是Page_one类的样子:

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() #make entry_one_var class instance variable
        entry_one = Entry(self, textvariable= self.entry_one_var) ##
        entry_one.grid()

我希望它有所帮助。