tkinter 网格不根据窗口大小调整大小 [英] tkinter grid not resizing based on window size

查看:28
本文介绍了tkinter 网格不根据窗口大小调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了一些我绝对无法弄清楚并需要帮助的东西.我有 2 个按钮,它们都将您发送到下一帧,它们工作得很好,但是当我调整窗口大小时,它们保持相同大小并保持在左上角,我希望它们将它们自己放在屏幕的中心.如果有一个关于他们成长的例子,那也将不胜感激,但不是必需的.如果可能的话,我也想坚持使用网格.我尝试了 columnconfigure 并将框架放在另一个框架中,我所做的一切似乎都不起作用.奇怪的是,如果我用 self 替换容器并将 self 放在它可以工作的按钮上但没有命令工作,有人可以向我解释一下或者给我一个很好的例子来说明我做错了什么吗?

So I have come across something I defiantly cant figure out and need help. I have 2 buttons that both send you to the next frame and they work just fine but when I resize the window they stay the same size and stay in the upper left corner and I want them to place them selves in the center of the screen. If there is an example as to them growing as well, that would also be appreciated but not necessary. Also I would like to stick with grid if possible. I tried columnconfigure and placing the frame in another frame, nothing i do seems to work. Strangely tho if I replace container with self and drop the self on the buttons it works but with out the command working, can someone explain this to me or give me a good example as to what I'm doing wrong?

代码示例:

import tkinter as tk
class MYapp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.grid(row = 0,column = 0, sticky = "nsew")
        container.rowconfigure(0, weight = 1)
        container.columnconfigure(0, weight = 1)
        container.columnconfigure(1, weight = 1)
        self.frames = {}
        for FRAME in (N1, N2):
            frame = FRAME(container, self)
            self.frames[FRAME] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")
        self.show_frame(N1)
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
class N1(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        a = tk.Button(self, text = "N1", command = lambda: controller.show_frame(N2))
        a.grid(row = 0, column = 0, sticky = "nsew")
        c = tk.Button(self, text = "N1", command = lambda: controller.show_frame(N2))
        c.grid(row = 0, column = 1, sticky = "nsew")
class N2(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        b = tk.Button(self, text = "N2")
        b.grid(row = 0, column = 0, sticky = "nsew")
if __name__ == "__main__":
    app = MYapp()
    app.mainloop()

已解决:感谢布莱恩.这是带有答案的更新代码.

SOLVED: Thanks to Bryan. This is the updated code with the answer in place.

import tkinter as tk
class MYapp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(fill="both", expand=True)
        container.rowconfigure(0, weight = 1)
        container.columnconfigure(0, weight = 1)
        self.frames = {}
        for FRAME in (N1, N2):
            frame = FRAME(container, self)
            self.frames[FRAME] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")
            frame.rowconfigure(0, weight = 1)
            frame.columnconfigure(0, weight = 1)
            frame.columnconfigure(1, weight = 1)
        self.show_frame(N1)
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
class N1(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        a = tk.Button(self, text = "N1", command = lambda: controller.show_frame(N2))
        a.grid(row = 0, column = 0, sticky = "nsew")
        c = tk.Button(self, text = "N1", command = lambda: controller.show_frame(N2))
        c.grid(row = 0, column = 1, sticky = "nsew")
class N2(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        b = tk.Button(self, text = "N2")
        b.grid(row = 0, column = 0, sticky = "nsew")
if __name__ == "__main__":
    app = MYapp()
    app.mainloop()

推荐答案

你不知道首选的行为是什么,只是它正在做的事情是错误的.我不知道这个建议是否会如你所愿.

You didn't way what the preferred behavior is, only that what it's doing is wrong. I don't know if this suggestion will do what you want or not.

根据经验,当您使用 grid 时,您应该始终为包含正在管理的小部件的父级赋予至少一行和一列的权重.当您为容器中的小部件执行此操作时,您不会为根窗口执行此操作.因此,容器内部的小部件可能会正确调整大小,但您不会看到它,因为容器本身没有调整大小.

As a rule of thumb, when you use grid you should always give at least one row and one column a weight for the parent containing the widgets being managed. While you do that for widgets in the container, you don't do it for the root window. Therefore, widgets inside the container might be resizing properly, but you won't see it because the container itself isn't resizing.

如果您只想将一个小部件放入另一个小部件中,我建议您使用 pack,因为您可以在一行代码中完成所有操作.因此,我建议容器本身使用 pack 而不是 grid:

If you're only going to put a single widget inside another widget, I recommend using pack because you can do everything in a single line of code. Therefore, I recommend using pack rather than grid for the container itself:

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

此外,由于您的容器似乎只有一行和一列,您可能不想给第 1 列赋予权重.

Also, since your container seems to only have a single row and a single column, you probably don't want to give column 1 a weight.

最后,您还使用 grid 来管理每个页面内的小部件(N1N2),但同样,您忘记了为这些框架内的任何行或列赋予权重.

Finally, you are also using grid to manage the widgets inside each page (N1, N2), but again, you are forgetting to give a weight to any rows or columns inside those frames.

这篇关于tkinter 网格不根据窗口大小调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆