tkinter制作框架粘与窗口

时间:2018-05-21 15:11:51

标签: python python-3.x tkinter

我是新手,所以不确定我在哪里错了。当你从右下角拖出窗口时,我想让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()

4 个答案:

答案 0 :(得分:1)

在您当前的GUI设置中,使用包装可能是一个更好的主意:

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()

答案 1 :(得分:0)

您可以pack LabeframescrolledText使用此命令通过删除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()

答案 2 :(得分:0)

您可以通过向tab1添加权重和向textbox添加粘贴来允许您的文本框和第1帧展开。

使用grid()时,您需要使用columnconfig()rowconfig()为该帧提供权重,以便在窗口大小调整时进行扩展。

要使用框架展开文本框,您需要添加粘性参数,如下所示:

txtbox.grid(row=0, column=0, sticky="nswe")

见下面的代码。

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()

答案 3 :(得分:0)

给定窗口小部件内的网格​​列的宽度将等于其最宽单元格的宽度,网格行的高度将是其最高单元格的高度。如果小部件没有完全填充单元格,则小部件上的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()