[英]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.在代码中,我在顶部有三个按钮,它们应该保持原样。
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.我读过可以使用columnconfigure
和rowconfigure
但不确定如何实现它。
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()
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). grid()
布局管理器的行号和列号从 0 开始,而不是从 1 开始(不过,开始将元素放置在您想要的任何位置并没有错,我只是提到这一点,因为您的代码给我的印象是您认为单元格开始在 1,而不是在 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.因为我不明白为什么开始将小部件放置在row=1
和column=1
,所以在下面的解决方案中,我开始放置在 0 索引处。
I suggest you to create a different container -parent widget- for the 3 buttons in questions (let us say a tkinter.Frame()
).我建议您为问题中的 3 个按钮创建一个不同的容器 - 父小部件(让我们说tkinter.Frame()
)。
Here is the code:这是代码:
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()
Demo:演示:
At not stretched:在未拉伸时:
Average stretching:平均拉伸:
After maximum stretching:最大拉伸后:
I tkink it would be better to not import everything of tkinter.我认为最好不要导入 tkinter 的所有内容。 And also puting funtion calls to main function.并且还将函数调用放入主函数。 Hence I just modify @Billal Begueradj 's pasted code to the following:因此,我只是将 @Billal Begueradj 的粘贴代码修改为以下内容:
# coding: utf-8
try:
import tkinter as tk
from tkinter import scrolledtext
except: # Python2 compatible
import Tkinter as tk
from Tkinter import scrolledtext
def main():
master_window = tk.Tk()
# Parent widget for the buttons
buttons_frame = tk.Frame(master_window)
buttons_frame.grid(row=0, column=0, sticky=tk.W+tk.E)
btn_Image = tk.Button(buttons_frame, text='Image')
btn_Image.grid(row=0, column=0, padx=(10), pady=10)
btn_File = tk.Button(buttons_frame, text='File')
btn_File.grid(row=0, column=1, padx=(10), pady=10)
btn_Folder = tk.Button(buttons_frame, text='Folder')
btn_Folder.grid(row=0, column=2, padx=(10), pady=10)
# Group1 Frame ----------------------------------------------------
group1 = tk.LabelFrame(master_window, text="Text Box", padx=5, pady=5)
group1.grid(row=1, column=0, columnspan=3, padx=10, pady=10, sticky=tk.E+tk.W+tk.N+tk.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=tk.E+tk.W+tk.N+tk.S)
master_window.mainloop()
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.