简体   繁体   English

扩展Tkinter框架

[英]Expand Tkinter Frame

I have a basic application using tkinter with python (3.5). 我有一个使用tkinter和python(3.5)的基本应用程序。 I want the application to be run in full screen, and have multiple windows to switch through. 我希望该应用程序在全屏模式下运行,并具有多个可切换的窗口。 So far this is what I have. 到目前为止,这就是我所拥有的。

import tkinter as tk

class Window(tk.Tk):

def __init__(self):
    tk.Tk.__init__(self)

    self.title("Movie Kiosk")
    self.attributes("-fullscreen", True)
    self.resizable(width=False, height=False)

    container = tk.Frame(self)
    container.pack(side="top", fill="both", expand=1)

    self.frames = {}

    for f in (StartPage, PageOne):
        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(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text="Main Page", font=("Verdana",48))
    label.place(relx=0.5, y=0, anchor=tk.N)
    button = tk.Button(self, text="Go to page 1",
                       command=lambda: controller.show_frame(PageOne))
    button.place(relx=1, rely=1, anchor=tk.SE)
    exitButton = tk.Button(self, text="Close Program", command=exit)
    exitButton.place(relx=0, rely=1, anchor=tk.SW)

class PageOne(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text="Page 1")
    label.pack()
    button = tk.Button(self, text="Back to home",
                       command=lambda: controller.show_frame(StartPage))
    button.pack()


app = Window()
app.mainloop()

When I run the application, the program loads in full screen mode, however the frame and all its widgets are packed tightly in the top left corner of the screen. 当我运行该应用程序时,该程序以全屏模式加载,但是该框架及其所有小部件都紧密包装在屏幕的左上角。 Not sure why this is happening, I have messed around changing properties of my my "app" and my frames. 不知道为什么会这样,我已经改变了我的“应用”和框架的属性。 If someone could tell me whats wrong or direct me to a place where I can find an answer it would be very much so appreciated. 如果有人可以告诉我出了什么问题或将我引导到可以找到答案的地方,那将非常感谢。 Thanks. 谢谢。

Not sure why this fixes it... 不确定为什么可以解决这个问题...

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

Adding this code after packing the container fixes the problem. 包装容器后添加此代码可解决此问题。

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

相关问题 如何在tkinter中扩展画布内部的框架? - How to expand a frame inside a canvas in tkinter? Python TKinter:调整窗口大小时,框架和画布将水平扩展,但不会垂直扩展 - Python TKinter: frame and canvas will expand horizontally, but not vertically, when window is resized Tkinter 框架扩展只显示我的窗口的一半 - Tkinter frame expand is only displaying half my window 展开文本小部件以在Tkinter中填充整个父框架 - Expand Text widget to fill the entire parent Frame in Tkinter Tkinter:如何让画布窗口中的框架扩展到画布的大小? - Tkinter: How to get frame in canvas window to expand to the size of the canvas? 如何将 tkinter treeview 列扩展到 window 框架? - How to expand tkinter treeview column past window frame? 在Tkinter中单击按钮或复选框时,如何扩展窗口/框架? - How to expand window/frame when button or checkbox is clicked in tkinter? 如何使多个文本小部件扩展以填充 Tkinter 中的框架 - How to make multiple text widgets expand to fill frame in Tkinter 如何让 tkinter 框架填充整个画布并与其一起扩展? - How do I make tkinter frame fill entire canvas and expand together with it? Tkinter:Frame 不会扩展以使用 grid() 填充根小部件内的剩余空间 - Tkinter: Frame does not expand to fill remaining space inside root widget with grid()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM