简体   繁体   English

在不同大小的帧之间切换

[英]Switching between differently sized Frames

How can I make these pages two different sizes?我怎样才能使这些页面有两种不同的尺寸? Say I want to make StartPage large, 500 x 500, and New_Card_Page small, say 100 x 100. App.geometry("200x200") changes all pages, which is not what I want.假设我想让StartPage大,500 x 500, New_Card_Page小,比如 100 x 100。 App.geometry("200x200")更改所有页面,这不是我想要的。

from tkinter import *
from tkinter import ttk
LARGE_FONT=('Verdana', 12)

class Index_Cards_App(Tk):   
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        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 = {}
        for F in (StartPage, New_Card_Page):
            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):
        Frame.__init__(self, parent)
        label=Label(self, text='Index Cards')  
        label.pack(pady=10,padx=10)               
        button1 = ttk.Button(self, text='Enter New Card', command=lambda: controller.show_frame(New_Card_Page))
        button1.pack()      
        
class New_Card_Page(Frame): 
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label=ttk.Label(self, text='Enter New Card') 
        label.pack(pady=10,padx=10)          
        button1 = ttk.Button(self, text='Back to Home', command=lambda: controller.show_frame(StartPage)).pack() 
        
app=Index_Cards_App()
app.geometry("200x200")
app.mainloop()       

The code you copied works by stacking all of the pages on top of each other, and assumes they are all the same size.您复制的代码的工作原理是将所有页面堆叠在一起,并假设它们的大小都相同。 It's not exactly a great design, especially if you're just learning tkinter.这不是一个很好的设计,特别是如果你只是在学习 tkinter。

If you want each page to have its own size, the easiest solution is to remove the use of grid when the pages are initially created, and instead have show_frame remove the current page and add the new page.如果您希望每个页面都有自己的大小,最简单的解决方案是在最初创建页面时删除grid的使用,而是让show_frame删除当前页面并添加新页面。

First, create an attribute to hold the current page so that it can be removed, and remove the call to grid inside the loop in __init__ .首先,创建一个属性来保存当前页面,以便它可以被删除,并在__init__的循环内删除对grid的调用。 Also, since we're not going to rely on stacking the pages on top of each other we can remove commands to configure the grid.此外,由于我们不依赖于将页面堆叠在一起,我们可以删除配置网格的命令。

def __init__(self, *args, **kwargs):
    Tk.__init__(self, *args, **kwargs)
    container = Frame(self)
    container.pack(side='top', fill='both', expand=True)
    self.current_frame = None
    self.frames = {}
    for F in (StartPage, New_Card_Page):
        frame = F(container, self)
        self.frames[F] = frame
    self.show_frame(StartPage)

Next, modify show_frame to hide the current page and show the new page.接下来,修改show_frame以隐藏当前页面并显示新页面。 Since we're no longer stacking widgets on top of each other, pack is a simpler choice than grid :由于我们不再将小部件堆叠在一起,因此pack是比grid更简单的选择:

def show_frame(self, cont):
    if self.current_frame is not None:
        self.current_frame.pack_forget()
    self.current_frame = self.frames[cont]
    self.current_frame.pack(fill="both", expand=True)

Finally, remove the call to geometry at the end of your code, since that forces the window to a specific size.最后,删除代码末尾对geometry的调用,因为这会强制窗口达到特定大小。

With those changes, each page will cause the window to resize to fit the current page.通过这些更改,每个页面都会导致窗口调整大小以适合当前页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM