创建从一个类到另一个类的变量

时间:2018-12-31 16:24:56

标签: python python-3.x class

我目前正在与tkinter一起进行学校分配,为用户输入输入内容创建了图形用户界面。我决定将输入分成不同的页面,以免让用户不知所措,也不需要滚动。
每个页面都有一系列的Label和Entry,分别在左右除数上分开,并且我设法以某种方式设法使它在每个页面上起作用。这是我的工作代码的简化版本:

import tkinter as tk


class Layers(tk.Frame):
    def __init__(self, root):
        super().__init__(root)

        self.layers = []

        self.layers.append(Welcome_Page(self))
        self.layers.append(Form_1(self))
        self.layers.append(Form_2(self))

        for layer in self.layers:
            layer.add_form(self)
            layer.add_buttons(self)
            layer.grid(row=0, column=0, sticky="nsew")

        self.layers[0].tkraise()


class Welcome_Page(tk.Frame):
    def __init__(self, root):
        super().__init__(root, width=600, height=800, background="red")

    def add_buttons(self, root):
        self.next = tk.Button(self, text="Next page", width=25, height=5, command=self.master.layers[1].tkraise)
        self.next.place(relx=1, rely=1, anchor="se")

        self.prev = tk.Button(self, text="Quit", width=25, height=5, command=self.master.master.destroy)
        self.prev.place(relx=0, rely=1, anchor="sw")
        pass

    def add_form(self, root):
        self.text_label = tk.Label(self, text="Welcome to this program")
        self.text_label.place(relx=0.5, rely=0, anchor="n")
        pass



class Form_1(tk.Frame):
    def __init__(self, root):
        super().__init__(root, width=600, height=800, background="yellow")

    def add_buttons(self, root):
        self.next = tk.Button(self, text="Next page", width=25, height=5, command=self.master.layers[2].tkraise)
        self.next.place(relx=1, rely=1, anchor="se")

        self.prev = tk.Button(self, text="Back", width=25, height=5, command=self.master.layers[0].tkraise)
        self.prev.place(relx=0, rely=1, anchor="sw")
        pass

    def add_form(self, root):
        self.text_label = tk.Label(self, text="Personal data")
        self.text_label.place(relx=0.5, rely=0, anchor="n")

        self.container_left = tk.Frame(self, background="#BAFFCE")
        self.container_right = tk.Frame(self, background="#72FF9A")
        self.container_left.grid(row=0, column=0, sticky="nsew")
        self.container_right.grid(row=0, column=1, sticky="nsew")
        self.grid_columnconfigure(0, weight=1, uniform="group1")
        self.grid_columnconfigure(1, weight=1, uniform="group1")
        self.grid_rowconfigure(0, weight=1)

        self.last_name_label = tk.Label(self.container_right, text="Last name")
        self.last_name_space = tk.Entry(self.container_right, text="lastname")
        self.last_name_label.grid(row=0, column=0, padx=(10,0), pady=(10,0))
        self.last_name_space.grid(row=0, column=1, padx=(5, 0), pady=(10,0))

        pass


class Form_2(tk.Frame):
    def __init__(self, root):
        super().__init__(root, width=600, height=800, background="gray")

    def add_buttons(self, root):
        self.next = tk.Button(self, text="Next page", width=25, height=5)
        self.next.place(relx=1, rely=1, anchor="se")

        self.prev = tk.Button(self, text="Back", width=25, height=5, command=self.master.layers[1].tkraise)
        self.prev.place(relx=0, rely=1, anchor="sw")
        pass

    def add_form(self, root):
        self.text_label = tk.Label(self, text="Third page")
        self.text_label.place(relx=0.5, rely=0, anchor="n")
        pass


if __name__ == '__main__':
    root = tk.Tk()
    root.geometry("600x800")

    window = Layers(root)
    window.pack(expand=True, fill="both")
    root.mainloop()

尽管如此,在将每个页面分成两个不同的Frame()容器时,我偶然发现了两个问题:

  • 使用ipadx和ipady设置框架的内部填充似乎没有任何作用。不过,我已经使用padx和pady手动将其内部的每个元素设置在其位置,并且效果很好,但是我相信我应该能够在container_left和container_right上对此使用内部填充。
  • 为每个页面设置容器是多余的,因为它们将全部分成两个框架。我已经尝试了以下方法,但是它不符合我的预期(根本没有)。
class Layers(tk.Frame):
    def __init__(self, root):
        super().__init__(root)

        self.layers = []

        self.layers.append(Welcome_Page(self))
        self.layers.append(Form_1(self))
        self.layers.append(Form_2(self))

        for layer in self.layers:
            layer.add_form(self)
            layer.add_buttons(self)
            layer.grid(row=0, column=0, sticky="nsew")

            layer.container_left = tk.Frame(layer, background="#BAFFCE")
            layer.container_right = tk.Frame(layer, background="#72FF9A")
            layer.container_left.grid(row=0, column=0, sticky="nsew")
            layer.container_right.grid(row=0, column=1, sticky="nsew")
            layer.grid_columnconfigure(0, weight=1, uniform="group1")
            layer.grid_columnconfigure(1, weight=1, uniform="group1")
            layer.grid_rowconfigure(0, weight=1)

            print(layer)

        self.layers[0].tkraise()

我遇到的错误是AttributeError: 'Form_1' object has no attribute 'container_right'。我得到的是,即使我使用的是layer.,也没有在类内部创建变量,而是在其他地方创建了变量。如何在类内创建变量,而又不重复使用代码?

由于我对Python和Tkinter还是很陌生,因此也提出了其他建议。

2 个答案:

答案 0 :(得分:0)

通过将第17行更改为:

,您可以在第一个代码中看到ipadxipady的效果。

layer.grid(row=0, column=0, sticky="nsew", ipadx=30, ipady=30,)

答案 1 :(得分:0)

我知道您提到过要保持类结构相同,但是为表单页面引入超类可能是值得的。从那里,您可以定义add_formadd_buttons实例方法,而仅从超类的__init__ 内部调用它们,而不用循环浏览页面。因此,就像:

class FormPage(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent, width=600, height=800, background="yellow")
        self.parent=parent
    
        self.add_buttons()
        self.add_form()

    def add_buttons(self):
        self.next = tk.Button(self, text="Next page", width=25, height=5)
        self.next.place(relx=1, rely=1, anchor="se")

        self.prev = tk.Button(self, text="Back", width=25, height=5)
        self.prev.place(relx=0, rely=1, anchor="sw")

    def add_form(self):
        self.text_label = tk.Label(self) #use the configure method in child classes to set the text
        self.text_label.place(relx=0.5, rely=0, anchor="n")

        self.container_left = tk.Frame(self, background="#BAFFCE")
        self.container_right = tk.Frame(self, background="#72FF9A")
        self.container_left.grid(row=0, column=0, sticky="nsew")
        self.container_right.grid(row=0, column=1, sticky="nsew")
        self.grid_columnconfigure(0, weight=1, uniform="group1")
        self.grid_columnconfigure(1, weight=1, uniform="group1")
        self.grid_rowconfigure(0, weight=1)

因此,FormPage的初始化将在实例化表单页面时自动调用add_buttonsadd_form方法。 然后,对于特定的表格,您可以执行以下操作:

class Form_1(FormPage):
    def __init__(self, parent):
        tk.Frame.__init__(parent) #call FormPage's initialisation

    def add_buttons(self):
        self.next.configure(command=self.parent.layers[1].tkraise)
        ...#add the specific functionality of the buttons using configure
    
    def add_form(self):
        super().add_form()
        self.text_Label.configure(text="Personal Information")

        self.last_name_label = tk.Label(self.container_right, text="Last name")
        self.last_name_space = tk.Entry(self.container_right, text="lastname")
        self.last_name_label.grid(row=0, column=0, padx=(10,0), pady=(10,0))
        self.last_name_space.grid(row=0, column=1, padx=(5, 0), pady=(10,0))

因此,FormPage的任何子级都具有属性container_leftcontainer_right。 然后,如果某些表单需要更多按钮,则可以重写该表单类中的方法add_buttons。同样,对于任何其他方法。 然后,您只需要一个位置来存储所有页面,例如Layers类。 IMO,您无需将所有图层都放在网格上,因为无论如何您都将通过导航按钮调用tkraise。我认为您的Layers类可以简化为:

class Layers(tk.Tk):
    def __init__(self):
        super().__init__(self)

        self.layers = []

        for F in {Welcome_Page, Form_1, Form_2}:
            self.layers.append(F(self))

        self.layers[0].tkraise()

通常,继承层次结构是减少代码重复的好方法。希望这会有所帮助:)