[英]tkinter making frame sticky with window
I am new to this so not sure where I am going wrong here. 我对此很陌生,所以不确定在这里我哪里出错了。 I want to make my
Frame_1
stick to the four corners of the window as you drag it out from the bottom right hand corner. 当您将
Frame_1
从右下角拖出时,我想使其Frame_1
在窗口的四个角上。
from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk
window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0)
tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0)
tab_control.grid(row=0, column=0, sticky=NSEW)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.grid(row=0, column=0, padx=10, pady=10, sticky=NSEW)
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.grid(row=0, column=0)
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)
window.mainloop()
In your current GUI set up, using pack throughout may be a better idea: 在当前的GUI设置中,最好在整个过程中使用pack:
import tkinter as tk
from tkinter import scrolledtext
from tkinter import ttk
if __name__ == '__main__':
window = tk.Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = tk.Frame(tab_control)
tab1.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
tab2 = tk.Frame(tab_control)
tab2.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
tab_control.pack(fill=tk.BOTH, expand=True)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = tk.LabelFrame(tab1, text="Frame_1")
labe1frame_1.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
window.mainloop()
You can pack
your Labeframe
and scrolledText
with this command to achieve that pack(expand=True, fil=BOTH)
by removing the grid
geometry layout manager. 你可以
pack
你Labeframe
和scrolledText
用这个命令来实现这一pack(expand=True, fil=BOTH)
通过去除grid
几何布局管理器。
from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk
window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0)
tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0, sticky=NSEW)
tab_control.grid(row=0, column=0, columnspan=3, padx=10, pady=10, sticky=E+W+N+S)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.pack(expand=True, fil=BOTH)
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.pack(expand=True, fil=BOTH)
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)
window.mainloop()
You can allow your textbox and Frame 1 to expand by adding a weight to tab1
and a sticky to textbox
. 您可以通过向
tab1
添加权重和向textbox
添加粘性来允许文本框和第1帧展开。
When using grid() you will want to use columnconfig()
and rowconfig()
to provide weights to that frame so it can expand with the window resizing. 使用grid()时,您将要使用
columnconfig()
和rowconfig()
为该框架提供权重,以便它可以随着窗口大小的调整而扩展。
For the textbox to expand with the frame you will need to add the sticky argument also like this: 为了使文本框随框架扩展,您还需要添加粘滞参数,如下所示:
txtbox.grid(row=0, column=0, sticky="nswe")
See below code. 参见下面的代码。
from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk
window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0)
tab1.columnconfigure(0, weight=1) # added weight
tab1.rowconfigure(0, weight=1) # added weight
tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0)
tab_control.grid(row=0, column=0, sticky="nswe")
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.grid(row=0, column=0, padx=10, pady=10, sticky="nswe")
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.grid(row=0, column=0, sticky="nswe") # added sticky
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)
window.mainloop()
The width of a grid column inside a given widget will be equal to the width of its widest cell, and the height of a grid row will be the height of its tallest cell. 给定小部件内的网格列的宽度将等于其最宽单元格的宽度,而网格行的高度将为其最高单元格的高度。 The sticky attribute on a widget controls only where it will be placed if it doesn't completely fill the cell.
小部件上的sticky属性仅控制未完全填充单元格的位置。
from tkinter import *
from tkinter import scrolledtext
from tkinter import ttk
window = Tk()
window.title("My Program")
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab1.grid(row=0, column=0, sticky=NSEW) #add sticky option
tab2 = ttk.Frame(tab_control)
tab2.grid(row=0, column=0)
tab_control.grid(row=0, column=0, sticky=NSEW)
tab_control.add(tab1, text='First')
tab_control.add(tab2, text='Second')
labe1frame_1 = LabelFrame(tab1, text="Frame_1")
labe1frame_1.grid(row=0, column=0, padx=10, pady=10, sticky=NSEW)
txtbox = scrolledtext.ScrolledText(labe1frame_1, width=40, height=10)
txtbox.grid(row=0, column=0, sticky=NSEW) #add sticky option
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
labe1frame_1.rowconfigure(0, weight=1)
labe1frame_1.columnconfigure(0, weight=1)
#configure the row and column size of parent window
tab1.columnconfigure(0,weight=3)
tab1.rowconfigure(0,weight=3)
window.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.