是否可以从一个类访问一个文本框条目(并追加到另一个类)?在这种情况下,它将来自第2页

时间:2019-07-08 16:31:59

标签: python python-3.x tkinter

我找不到一种方法来访问此文本框,并使用Tkinter从另一个类将其追加到该文本框。

我无法使用以前使用的方法(通过调用其他条目StringVar()),这导致我的编程停顿了。由于无法将文本框命名为StringVar(),因此我对如何执行此操作感到困惑。

import tkinter as tk

class MainWindow(tk.Tk):

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

        tk.Tk.__init__(self, *args, **kwargs)
        self.geometry("1350x750")

        container = tk.Frame(self)
        container.grid(row=0, column=0)

        container.config(bg="gray20")

        self.frames = {}

        for F in (ReservationPage, ReceiptPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0)

        self.show_frame(ReservationPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class ReservationPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.MainFrame = tk.Frame(self, width=1350, height=700, bg="gray20")
        self.MainFrame.grid(row=0,column=0)
        self.MoorFrame = tk.LabelFrame(self, bg="gray20", width=300, height=150)
        self.MoorFrame.place(x=450, y=120)

        ReceiptPageButton1 = tk.Button(self, text="Receipts", font=("Helvetica", 20, "bold"),bg="gray20", fg="light grey",
                                       bd=0,command=lambda: controller.show_frame(ReceiptPage))
        ReceiptPageButton1.place(x=100, y=30)


class ReceiptPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.MainFrame = tk.Frame(self, width=1350, height=700, bg="gray20")
        self.MainFrame.grid(row=0,column=0)

        ReservationPageButton1 = tk.Button(self, text="Reservations", font=("Helvetica", 20, "bold"),bg="gray20", fg="light grey",
                                       bd=0,command=lambda: controller.show_frame(ReservationPage))
        ReservationPageButton1.place(x=80, y=30)

        ReceiptBox = tk.Text(self, height=20, width=137, font=("Helvetica",11,"bold"))
        ReceiptBox.place(x=27, y=165)

1 个答案:

答案 0 :(得分:0)

这里是一个例子。注意,我还删除了许多其他无用的代码。我建议您不要做的一件事就是停止使用place()。学会改用grid()pack()。学习起来有点困难,但是一旦您知道它们是如何工作的,那么编程就容易得多了,因为tkinter会为您找出所有这些像素位置。

import tkinter as tk

bg_color = "gray20"
button_style = dict(
    font=("Helvetica", 20, "bold"),
    bg=bg_color,
    fg="light grey",
    bd=0)

class MainWindow(tk.Tk):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.geometry("1350x750")

        self.frames = {}
        for F in (ReservationPage, ReceiptPage):
            frame = F(self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.show_frame(ReservationPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class ReservationPage(tk.Frame):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, bg=bg_color, **kwargs)
        self.MoorFrame = tk.LabelFrame(self, bg=bg_color, width=300, height=150)
        self.MoorFrame.place(x=450, y=120)

        ReceiptPageButton1 = tk.Button(self, text="Receipts", command=lambda: parent.show_frame(ReceiptPage), **button_style)
        ReceiptPageButton1.place(x=100, y=30)

        test_button = tk.Button(self, text="Test add text", command=self.add, **button_style)
        test_button.place(x=100, y=100)

    def add(self):
        textbox = self.master.frames[ReceiptPage].ReceiptBox
        textbox.insert(tk.END, "This is only a test\n")

class ReceiptPage(tk.Frame):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, bg=bg_color, **kwargs)

        ReservationPageButton1 = tk.Button(self, text="Reservations", command=lambda: parent.show_frame(ReservationPage), **button_style)
        ReservationPageButton1.place(x=80, y=30)

        # using a name that starts with "self." makes this an instance variable
        self.ReceiptBox = tk.Text(self, height=20, width=137, font=("Helvetica",11,"bold"))
        self.ReceiptBox.place(x=27, y=165)

def main():
    root = MainWindow()
    root.mainloop()

if __name__ == '__main__':
    main()