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

查看:133
本文介绍了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()

已解决: 多亏布莱恩(Bryan). 这是更新的代码,并且有相应的答案.

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天全站免登陆