如何正确地从pack()切换到place()

时间:2019-01-06 18:32:58

标签: python-3.x user-interface tkinter ttk

我将tkinter gui划分为不同的类,并希望通过按钮在主屏幕中将它们连接起来,就像Steven M. Vascellaro在这里所做的:Switch between two frames in tkinter

import tkinter.ttk as ttk
#from alexa_site_2 import Frame_alexa
from globals import Globals
from tkinter import PhotoImage
import tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.geometry('{}x{}'.format(1024, 600))
        self.configure(bg="white")
        for i in range(len(Globals.image_files)):
            image_box = PhotoImage(file=Globals.image_files[i])
            Globals.list_images.append(image_box)
        self.switch_frame(StartPage)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.place(x=110,y=0)
        print("switch frame function")

class StartPage(ttk.Frame):
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        #self.place(x=0,y=0)
        ttk.Label(self, text="This is the start page").pack()
        ttk.Button(self, text="Open page one",
                  command=lambda: master.switch_frame(PageOne)).pack()
        ttk.Button(self, text="Open page two",
                  command=lambda: master.switch_frame(PageTwo)).pack()
    ttk.Button(self, text="Open page two",
      command=lambda: master.switch_frame(Frame_alexa)).pack()

class PageOne(ttk.Frame):
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        ttk.Label(self, text="This is page one").pack(side="top", fill="x", pady=10)
        ttk.Button(self, text="Return to start page",
              command=lambda: master.switch_frame(StartPage)).pack()

class PageTwo(ttk.Frame):
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        ttk.Label(self, text="This is page two").place(x=40,y=10)
        ttk.Button(self, text="Return to start page",
              command=lambda: master.switch_frame(StartPage)).place(x=0,y=0)

if __name__ == "__main__":

    app = SampleApp()
    app.mainloop()

当我尝试连接自己的类时,结果得到一个白色的窗口。您可以在pageOne中看到它的运行情况,并在PageTwo中看到我的问题。我不打算混合place()和pack()。我只需要place()和您的帮助。

0 个答案:

没有答案