Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 调整多个栅格的大小时遇到问题_Python_Tkinter - Fatal编程技术网

Python 调整多个栅格的大小时遇到问题

Python 调整多个栅格的大小时遇到问题,python,tkinter,Python,Tkinter,我有以下代码。我的问题是我无法正确调整框架的大小。当我运行程序时,一切都如预期的那样。但是,当我调整它的大小时,我希望保留原始视图 from Tkinter import * import os import sys ALL=N+S+E+W class Application(Frame): def __init__(self,master=None): Frame.__init__(self,master) self.master.rowconf

我有以下代码。我的问题是我无法正确调整框架的大小。当我运行程序时,一切都如预期的那样。但是,当我调整它的大小时,我希望保留原始视图

from Tkinter import *
import os
import sys


ALL=N+S+E+W

class Application(Frame):

    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.master.rowconfigure(0,weight=1)
        self.master.columnconfigure(0,weight=1)
        self.grid(sticky=ALL)

        self.rowconfigure(0,weight=1)
        myframe1=Frame(self,bg='green')
        myframe1.bind("<Button-1>",self.handler1)
        myframe1.grid(row=0,column=0,rowspan=1,columnspan=2,sticky=ALL)
        self.rowconfigure(1,weight=1)
        myframe2=Frame(self,bg='blue')
        myframe2.bind("<Button-1>",self.handler2)
        myframe2.grid(row=1,column=0,rowspan=1,columnspan=2,sticky=ALL)


        buttons=('Red','Blue','Green','Black')
        button=[0]*4
        for c in range(4):
            self.rowconfigure(c+2,weight=1)
            self.columnconfigure(c,weight=1)
            button[c]=Button(self,text="{0}".format(buttons[c]),command=lambda     x=buttons[c]:self.colors(x))
            button[c].grid(row=2,column=c,sticky=E+W)

        self.columnconfigure(4,weight=1)
        self.rowconfigure(6,weight=1)
        button1=Button(self,text='{0}'.format('Open'),command=self.content)
        button1.grid(row=2,column=4,sticky=E+W)


        f=Frame(self,bg='red')
        self.myentry=Entry(f)
        self.myentry.grid(row=0,column=4,sticky=ALL)
        self.text=Text(f)
        self.text.grid(row=1,column=4,sticky=ALL)
        f.grid(row=0,column=2,rowspan=2,columnspan=3,sticky=ALL)

        ...
从Tkinter导入*
导入操作系统
导入系统
全部=N+S+E+W
课程申请(框架):
def uuu init uuu(self,master=None):
帧。\uuuu初始化(自,主)
self.master.rowconfigure(0,权重=1)
self.master.columnconfigure(0,权重=1)
self.grid(粘性=全部)
self.rowconfigure(0,权重=1)
myframe1=帧(self,bg='green')
myframe1.bind(“,self.handler1)
myframe1.grid(行=0,列=0,行span=1,列span=2,粘滞=ALL)
self.rowconfigure(1,权重=1)
myframe2=帧(self,bg='blue')
myframe2.bind(“,self.handler2)
myframe2.grid(行=1,列=0,行span=1,列span=2,粘性=ALL)
按钮=(‘红色’、‘蓝色’、‘绿色’、‘黑色’)
按钮=[0]*4
对于范围(4)内的c:
self.rowconfigure(c+2,权重=1)
self.columnconfigure(c,权重=1)
按钮[c]=按钮(self,text=“{0}”。格式(按钮[c]),命令=lambda x=buttons[c]:self.colors(x))
按钮[c]。网格(行=2,列=c,粘性=E+W)
self.columnconfigure(4,权重=1)
self.rowconfigure(6,权重=1)
button1=按钮(self,text='{0}'。格式('Open'),命令=self.content)
按钮1.网格(行=2,列=4,粘性=E+W)
f=帧(自身,背景='红色')
self.myentry=条目(f)
self.myentry.grid(行=0,列=4,粘滞=ALL)
self.text=text(f)
self.text.grid(行=1,列=4,粘滞=ALL)
f、 网格(行=0,列=2,行span=2,列span=3,粘性=ALL)
...
我尝试了许多组合,包括
行配置
列配置
行span
列span
,但失败了

我的原意是:

在一个方向上调整大小后:

另一方面:


白色区域是我想要调整大小的
文本
小部件(蓝色和绿色区域)。

您的问题是您似乎不太了解网格是如何工作的。例如,您只将两个小部件放在红色框中(self.myentry和self.text),而将它们放在第2列和第4列中。您是否知道这些列是相对于其父列的,而不是相对于整个GUI的?您希望它们位于红色边框的第0列,然后希望红色边框位于其父列的第二列

解决这个问题的办法是分而治之。首先,将主屏幕划分为逻辑部分,并对这些逻辑部分进行布局,以便它们能够正确调整大小。然后,对每个零件内部的任何东西,用肥皂冲洗,然后重复。使用框架进行组织是一种方法

以下是我将如何解决您的问题(尽管解决此问题的方法肯定不止一种)。首先,屏幕有两个主要区域:顶部有绿色、蓝色和红色框架及其内容,底部有按钮。顶部区域应在各个方向上增长和收缩,底部区域仅在X方向上增长。我将为此创建两个框架,每个部分一个,并使用pack,因为pack是最简单的几何体管理器。顶部框架应配置为填充两个方向并展开。底部部分(带按钮)应仅填充X方向

现在有两个区域相互独立,并具有适当的调整大小行为:“主”区域和“工具栏”区域。您可以随意安排这些框架的内部内容,而不必担心这会如何影响主布局

在底部框架中,如果您希望所有小部件大小相同,请使用pack并让它们全部填充X和展开,它们将均匀地填充该区域。如果希望它们的大小不同,请使用栅格,以便可以单独控制每一列

对于顶部,它有三个子部分:红色、绿色和蓝色框架。因为它们不是水平或垂直排列的,所以我会使用网格。在单元格0,0中放置绿色,在单元格0,1中放置蓝色,在单元格1,1中放置红色,跨越两行。为第0行和第1列指定权重1,使其占据所有松弛部分

正如我之前所写的,这不是“分而治之”这个具体问题的唯一方法。与将主应用程序视为两部分(顶部和底部,顶部有三个子部分)不同,另一个选择是查看主窗口有四个部分:绿色、蓝色、红色和工具栏。关键不在于选择完美的定义,而在于将布局问题分解为从外到内的各个部分

以下是一个工作示例:

from Tkinter  import *

ALL=N+S+E+W

class Application(Frame):

    def __init__(self,master=None):
        Frame.__init__(self,master)

        # the UI is made up of two major areas: a bottom row
        # of buttons, and a top area that fills the result of 
        # UI
        top_frame = Frame(self)
        button_frame = Frame(self)

        button_frame.pack(side="bottom", fill="x")
        top_frame.pack(side="top", fill="both", expand=True)

        # top frame is made up of three sections: two smaller
        # regions on the left, and a larger region on the right
        ul_frame = Frame(top_frame, background="green", width=200)
        ll_frame = Frame(top_frame, background="blue", width=200)
        right_frame = Frame(top_frame, background="red")

        ul_frame.grid(row=0, column=0, sticky=ALL)
        ll_frame.grid(row=1, column=0, sticky=ALL)
        right_frame.grid(row=0, column=1, rowspan=2, sticky=ALL)
        top_frame.columnconfigure(1, weight=1)
        top_frame.rowconfigure(0, weight=1)
        top_frame.rowconfigure(1, weight=1)

        # the right frame is made up of two widgets, an entry
        # on top and a text below
        entry = Entry(right_frame)
        text = Text(right_frame)

        entry.pack(side="top", fill="x")
        text.pack(side="top", fill="both", expand=True)

        # the button frame has five equally spaced buttons
        for color in ('Red', 'Blue', 'Green', 'Black'):
            b = Button(button_frame, text=color)
            b.pack(side="left", fill="x", expand=True)
        quit_button = Button(button_frame, text="Quit")
        quit_button.pack(side="left", fill="x", expand=True)

root = Tk()
app = Application(root)
app.pack(fill="both", expand=True)
root.mainloop()

:谢谢你的指点。不过我没能把它做完!你说的是“你只把两个小部件放在红色框中(self.myentry和self.text),但你把它们放在第2列和第4列”。我把它们都放在第4列。当你说它们是与它们的父项相关时,你的意思是f.grid(row=0,column=2)?这意味着什么?我认为我的问题在于for循环。正如你从我的原始视图中看到的,布局很好。调整大小是一个问题(我不想要红色背景,文本和条目小部件必须在它的位置)我关于第2列和第4列的错误。尽管如此,当只有一个小部件时,将其放在第4列是没有意义的。您的问题不在循环中,它与行和列的权重以及列的错误使用有关。当你说
f.grid(row=0,…)
时,f被放在
self
的第0行(其父行)。当您说
self.text.grid(row=1,…)
时,这是f的第一行,因为它是f的父行。f在第0行中以零结尾。每个容器(
self
f
)都有自己的“网格”。可以这么说:好的,我更正了f帧,但仍然是一样的。按钮没问题,所以我还必须更改什么?我想不出来!:好的,很好!如我所见,你