Python - 带有网格管理器的多帧 [英] Python - Multiple frames with Grid manager

查看:21
本文介绍了Python - 带有网格管理器的多帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Tkinter 模块 (Python 2.7) 中的功能来创建一个 GUI,该 GUI 将八个小部件放置在 7 行 x 5 列网格上(抱歉,我没有包含图像;对话框是不允许我浏览和上传保存的图像).

I'm trying to use the functionality from the Tkinter module (Python 2.7) to create a GUI that has eight widgets placed on a 7 row by 5 column grid (sorry that I did not include an image; the dialog box is not allowing me to browse and upload the saved image).

(小部件、start_row、start_col、row_span、column_span):

(Widget, start_row, start_col, row_span, column_span):

  1. ("按钮 0", 6, 0, 1, 1)
  2. ("按钮 1", 6, 1, 1, 1)
  3. ("按钮 2", 6, 2, 1, 1)
  4. ("按钮 3", 6, 3, 1, 1)
  5. ("按钮 4", 6, 4, 1, 1)
  6. ("Frame1", 0, 0, 3, 2)
  7. ("Frame2", 2, 0, 3, 2)
  8. ("Frame3", 0, 3, 6, 3)

然而,当我运行我的代码时,按钮和 Frame3 渲染得很好,但 Frame1 垂直挤压"了 Frame2.任何建议将不胜感激.(我已阅读建议的 StackOverflow 答案,但似乎没有提供可用于解决我的问题的信息.此外,我在网上进行了广泛搜索,但无济于事.)

Yet, when I run my code, the buttons and Frame3 are rendered fine, but Frame1 vertically "squashes" Frame2. Any suggestions would be greatly appreciated. (I have read the suggested StackOverflow answers and none seem to provide information that can be used to solve my problem. Also, I have searched extensively online to no avail.)

from Tkinter import *

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.title("Grid Manager")

        self.master.rowconfigure(0, weight=1)
        self.master.columnconfigure(0, weight=1)

        for i in range(5):
            self.master.button = Button(master, text = "Button {0}".format(i))
            self.master.button.grid(row=6, column=i, sticky=W+E)

        self.Frame1 = Frame(master, bg="red")
        self.Frame1.grid(row = 0, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S) 
        self.Frame2 = Frame(master, bg="blue")
        self.Frame2.grid(row = 2, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S)
        self.Frame3 = Frame(master, bg="green")
        self.Frame3.grid(row = 0, column = 2, rowspan = 6, columnspan = 3, sticky = W+E+N+S)

root = Tk()
app = Application(master=root)
app.mainloop()

更新:现在我在我的个人电脑前,这里分别是我想要的结果和我得到的结果的图像:

UPDATE: Now that I'm at my personal computer, here are images for the result that I want and the result that I get, respectively:

推荐答案

在折腾了几个小时的代码之后,我终于能够创建我想要的 GUI.关键是遍历行和列并分别使用 rowconfigure 和 columnconfigure 设置它们的权重.代码如下:

After messing around with my code for a few hours, I was finally able to create the GUI that I intended to. The key was looping over rows and columns and setting their weights using rowconfigure and columnconfigure, respectively. Code is below:

from tkinter import *

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.master.title("Grid Manager")

        for r in range(6):
            self.master.rowconfigure(r, weight=1)    
        for c in range(5):
            self.master.columnconfigure(c, weight=1)
            Button(master, text="Button {0}".format(c)).grid(row=6,column=c,sticky=E+W)

        Frame1 = Frame(master, bg="red")
        Frame1.grid(row = 0, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S) 
        Frame2 = Frame(master, bg="blue")
        Frame2.grid(row = 3, column = 0, rowspan = 3, columnspan = 2, sticky = W+E+N+S)
        Frame3 = Frame(master, bg="green")
        Frame3.grid(row = 0, column = 2, rowspan = 6, columnspan = 3, sticky = W+E+N+S)

root = Tk()
root.geometry("400x200+200+200")
app = Application(master=root)
app.mainloop()

这篇关于Python - 带有网格管理器的多帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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