tkinter 使用主窗口调整框架和内容的大小 [英] tkinter resize frame and contents with main window
问题描述
我正在研究如何控制包含框架的窗口的大小调整.在代码中,我在顶部有三个按钮,它们应该保持原样.
I am trying to work out how to control resizing of the window containing a frame. In the code I have three buttons across the top that should to stay exactly where they are.
当我拖动窗口以展开它时,我希望框架和它包含的文本框随主窗口一起展开.我读过 columnconfigure
和 rowconfigure
可以使用,但不确定如何实现它.
When I drag the window to expand it, I want the frame and the text box it contains to expand with the master window. I have read that columnconfigure
and rowconfigure
can be used but not sure how to implement it.
from tkinter import *
from tkinter import scrolledtext
master_window = Tk()
# Create the buttons
btn_Image = Button(master_window, text='Image')
btn_Image.grid(row=1, column=1, padx=(10), pady=10, sticky=E + W)
btn_File = Button(master_window, text='File')
btn_File.grid(row=1, column=2, padx=(10), pady=10, sticky=E + W)
btn_Folder = Button(master_window, text='Folder')
btn_Folder.grid(row=1, column=3, padx=(10), pady=10, sticky=E + W)
# Group1 Frame ----------------------------------------------------
group1 = LabelFrame(master_window, text="Text Box", padx=5, pady=5)
group1.grid(row=2, column=1, columnspan=3, padx=10, pady=10, sticky=E+W+N+S)
# Create the textbox
txtbox = scrolledtext.ScrolledText(group1, width=40, height=10)
txtbox.grid(row=1, column=1, columnspan=3, sticky=E+W+N+S)
mainloop()
推荐答案
grid()
布局管理器的行列号从0开始,不是从1开始(没有错的开始将元素放在任何你想要的地方,我只是提到这一点,因为你的代码给我的印象是你认为单元格从 1 开始,而不是从 0 开始).因为我不明白为什么在 row=1
和 column=1
处开始放置小部件,所以在下面的解决方案中,我开始放置在 0 索引处.
The row and column numbers of the grid()
layout manager start from 0, not from 1 (there is nothing wrong to start placing the elements wherever you want though, I just mention this because your code gives me the impression you think the cells start at 1, not at 0). Because I do not see the reason why to start placing the widgets at row=1
and column=1
, in my solution below, I start placing at the 0 index.
我建议您为问题中的 3 个按钮创建一个不同的容器 - 父小部件(让我们说一个 tkinter.Frame()
).
I suggest you to create a different container -parent widget- for the 3 buttons in questions (let us say a tkinter.Frame()
).
代码如下:
from tkinter import *
from tkinter import scrolledtext
master_window = Tk()
# Parent widget for the buttons
buttons_frame = Frame(master_window)
buttons_frame.grid(row=0, column=0, sticky=W+E)
btn_Image = Button(buttons_frame, text='Image')
btn_Image.grid(row=0, column=0, padx=(10), pady=10)
btn_File = Button(buttons_frame, text='File')
btn_File.grid(row=0, column=1, padx=(10), pady=10)
btn_Folder = Button(buttons_frame, text='Folder')
btn_Folder.grid(row=0, column=2, padx=(10), pady=10)
# Group1 Frame ----------------------------------------------------
group1 = LabelFrame(master_window, text="Text Box", padx=5, pady=5)
group1.grid(row=1, column=0, columnspan=3, padx=10, pady=10, sticky=E+W+N+S)
master_window.columnconfigure(0, weight=1)
master_window.rowconfigure(1, weight=1)
group1.rowconfigure(0, weight=1)
group1.columnconfigure(0, weight=1)
# Create the textbox
txtbox = scrolledtext.ScrolledText(group1, width=40, height=10)
txtbox.grid(row=0, column=0, sticky=E+W+N+S)
mainloop()
演示:
未拉伸时:
平均拉伸:
最大拉伸后:
这篇关于tkinter 使用主窗口调整框架和内容的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!