我正在Tkinter中创建GUI。我已经一次又一次地尝试让我的小部件在GUI中得到扩展。我正在使用粘性命令和列/行权重。在“设计查看器”选项卡上,我希望列表框与窗口垂直拉伸,我希望带有蓝色框的画布垂直和水平拉伸。我添加了一张图片,以澄清我预期会发生什么
提前谢谢!这是我的密码:
# Gui
import tkinter as tk
from tkinter import N,W,S,E
from tkinter import messagebox
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.grid(sticky = N+E+S+W)
self.create_application_foundation()
def create_application_foundation(self):
# Create menubar ribbon
self.topRibbon = tk.Frame(self, bg = "#E3E5E6")
self.topRibbon.grid(row=0, column=0, sticky=W+E+N+S)
# Initialize Buttons
width = 15; height = 2; bg = "#F5F6F7"; relief = "flat"; bd = 0; padx = 1
self.designViewerButton = tk.Button(self.topRibbon, text = "Design\nViewer", width = width,
height = height, bg = bg, relief = relief, bd = bd)
self.patternCreatorButton = tk.Button(self.topRibbon, text = "Pattern\nCreator",width = width,
height = height, bg = bg, relief = relief, bd = bd)
# Add buttons to ribbon
self.designViewerButton.grid(row = 1, column = 0, padx = padx, sticky = N+W)
self.patternCreatorButton.grid(row = 1, column = 1, padx = padx, sticky = N+W)
# Create design viewer window
self.createdesignViewerWindow()
self.createObjectCreator()
# Try to make things expandable
self.topRibbon.grid_rowconfigure(1, weight=1)
self.topRibbon.grid_columnconfigure(1, weight=1)
def createdesignViewerWindow(self):
width = 30; height = 1; bg = "#F5F6F7"; relief = "flat"; bd = 0; pady = 1
# Create window
self.designViewerWindow = tk.Frame(self, bg = "#E3E5E6")
self.designViewerWindow.grid(row=1, column=0, sticky =W+E+N+S)
# Create object button
self.objectButton = tk.Button(self.designViewerWindow, text = "Object", width = width,
height = height, bg = bg, relief = relief, bd = bd)
self.objectButton.grid(row = 0, sticky =N+W, pady = pady)
# Create object listbox
self.objectListBox = tk.Listbox(self.designViewerWindow)
objects = ["Object {}".format(i) for i in range(50)]
self.objectListBoxFrame = tk.Frame(self.designViewerWindow)
self.objectsListBox = tk.Canvas(self.objectListBoxFrame, width=100, height=200)
self.objectsListBox.grid(row = 0, rowspan = 5,column = 0, sticky = N+S)
self.objectListBoxFrame.grid(row = 1, sticky = N+S, pady = pady)
# Create canvas with blue rectangle
self.imageCanvas = tk.Canvas(self.designViewerWindow, width=200, height=100)
self.imageCanvas.grid(row = 0, rowspan = 5,column = 1, columnspan = 5, sticky = N+E+S+W)
self.imageCanvas.create_rectangle(50, 25, 150, 75, fill="blue")
self.imageCanvas.config(bg = "white")
# Attempt to make window expandable
self.designViewerWindow.rowconfigure(1,weight = 1)
self.designViewerWindow.columnconfigure(1, weight=1)
def createObjectCreator(self):
self.objectCreatorWindow = tk.Frame(self,bg = "#E3E5E6")
self.tempLabel = tk.Label(self.objectCreatorWindow, text = "Object Creator").grid()
if __name__ == "__main__":
import sys
root = tk.Tk()
root.title("My Program")
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(1, weight=1)
# root.minsize(400,200)
app = Application(master=root)
app.mainloop()
问题的第一部分是,您尚未将应用程序配置为填充窗口,因此应用程序中的任何内容都不会填充窗口
您似乎试图这样做,但您将根窗口中的所有权重赋予了行和列1,但您将
app
放入了行和列0。您需要为行和列0指定权重:注意:如果一个容器中只有一个小部件,那么使用pack就更容易了,因为您可以将它配置为仅用一行代码而不是三行代码填充窗口
例如:
。。。对
下一个问题是,您正在使用
grid
将小部件放在Application
框架中,但是您没有为Application
框架中的任何行或列赋予权重。所以再一次,这个框架不会填充它的父框架,所以里面的任何小部件也不会填充窗口我认为您希望第0列获得所有额外空间,但希望第1行是获得额外空间的行,因为
self.designViewerWindow
就在那里将以下内容添加到
Application
的__init__
中:还有其他问题,但这表明了一个普遍的问题:您没有正确配置每组小部件的行和列。根据经验,对于每个具有由
grid
管理的子部件,您需要为至少一行和一列赋予正权重如果您将同一小部件的子项对
grid
的所有调用组合在一起,然后在同一位置添加rowconfigure
和columnconfigure
命令,那么这一切都会更容易看到。这样,您可以轻松地一眼就看到每个组的所有网格选项相关问题 更多 >
编程相关推荐