Tkinter-如何从另一个类中获取变量?

时间:2019-05-30 08:12:29

标签: python-3.x class tkinter

我想从首页输入中获取api和desti 并按标签将其打印在pageone上。 所以我尝试使其成为全局并将其存储在控制器中 但是两者都没有用... 我该怎么办?

import tkinter
from tkinter import *

LARGE_FONT = ("Verdana", 12)


class Travelary(Tk):

    def __init__(self):
        Tk.__init__(self)
        self.title("TRAVELARY")
        self.geometry("640x400+100+100")
        self.resizable(False, False)

        container = Frame(self)

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

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        self.app_data = {"api": StringVar(),
                         "desti": StringVar()}

        for F in (StartPage, PageOne, PageTwo):
            frame = F(container, self)

            self.frames[F] = frame

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

        self.show_frame(StartPage)

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


class StartPage(Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        Frame.__init__(self, parent)

        bg_img = PhotoImage(file="11.gif")  # 이미지 파일 위치
        wall_label = Label(self, image=bg_img)
        wall_label.image = bg_img  # keep a reference
        wall_label.place(x=0, y=0)

        lbl1 = Label(self, text="구글 연동을 위해 '구글 API 코드'가 필요합니다.")
        lbl1.pack()
        lbl2 = Label(self, text="구글 API 코드를 입력해 주세요.")
        lbl2.place(x=240, y=100)
        self.ent2 = Entry(self, textvariable=self.controller.app_data["api"], width=30)
        self.ent2.place(x=220, y=130)
        lbl3 = Label(self, text="여행지를 입력해 주세요.")
        lbl3.place(x=260, y=160)
        self.ent3 = Entry(self, textvariable=self.controller.app_data["desti"], width=30)
        self.ent3.place(x=220, y=190)

        btn1 = Button(self, text="submit", width=15,
                      command=lambda: controller.show_frame(PageOne))
        btn1.place(x=270, y=220)


class PageOne(Frame):

    def __init__(self, parent, controller):
        self.controller = controller
        api = self.controller.app_data["api"].get()
        destination = self.controller.app_data["desti"].get()

        Frame.__init__(self, parent)
        label = Label(self, text="%s !" % destination, font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = Button(self, text="Back to Home",
                         command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = Button(self, text="Page Two",
                         command=lambda: controller.show_frame(PageTwo))
        button2.pack()


app = Travelary()
app.mainloop()

我想用“!”标记“ desti” 但这只会给我带来“!” 我不知道如何从类中获取入口的参数 并在另一个类上使用它。

0 个答案:

没有答案