簡體   English   中英

添加小部件時 Tkinter 框架會調整大小

[英]Tkinter frame resizes when I add widgets

我剛開始使用 Tkinter,但面臨一個奇怪的問題,基本上,我希望我的應用程序看起來像這樣

在此處輸入圖片說明

因此,為了做到這一點,我創建了兩個Frames ,一個用於菜單項,另一個用於顯示內容。 奇怪的是,當我用給定的寬度和高度初始化框架時,程序似乎按預期工作,但是當我在窗口內放置一些小部件時,它看起來像這樣

在此處輸入圖片說明

有人可以向我解釋這種奇怪的行為嗎? 我錯過了什么? 另外,我想提一下,當我將按鈕添加到菜單框架時,寬度會改變以適應按鈕寬度,反之亦然

app = tk.Tk()
app.resizable(False, False)

menu_frame_users = tk.Frame(app, width=200, background='red')
content_frame = tk.Frame(app, height=600, width=600, background='blue')


hello_label = tk.Label(menu_frame_users, text='Hello, User').grid(column=0, row=0, sticky='we')
view_profile_button = tk.Button(menu_frame_users, text="View Profile").grid(column=0, row=1, sticky='we')
invoices_button = tk.Button(menu_frame_users, text="Invoices").grid(column=0, row=2, sticky='we')
bookings_button = tk.Button(menu_frame_users, text="View bookings").grid(column=0, row=3, sticky='we')
tools_button = tk.Button(menu_frame_users, text="Search tools").grid(column=0, row=4, sticky='we')

test_label = tk.Label(content_frame, text='View profile')
test_label.grid(column=0, row=0, sticky='we')


menu_frame_users.grid(column=0, row=0, sticky='nswe')
content_frame.grid(column=1, row=0, sticky='nswe')

tkinter 中的框架將僅與其中包含的小部件一樣大,除非您向行和列添加權重以使其擴展。

您可以通過設置窗口的大小,然后為適當的行和列添加權重來擴展框架。

    app.geometry('500x400')
    app.grid_columnconfigure(1, weight=1)
    app.grid_rowconfigure(0, weight=1)

您可以調整窗口的大小,然后調整按鈕的大小和位置,直到獲得所需的布局。

您還可以使用:

    app.grid_columnconfigure(1, minsize=300)

但是,這僅適用於列包含小部件時。

我不確定我是否完全理解這個問題,但也許這會有所幫助。 注意為了示例,我刪除了顯式框架尺寸。

import tkinter as tk

app = tk.Tk()
app.resizable(False, False)

menu_frame_users = tk.Frame(app,background='red')
content_frame = tk.Frame(app, background='blue')

app.geometry("500x500")

app.rowconfigure(0, weight=1)
app.columnconfigure(0, weight=1)
menu_frame_users.columnconfigure(0, weight=1)

hello_label = tk.Label(menu_frame_users, text='Hello, User').grid(column=0, row=0, sticky='we')
view_profile_button = tk.Button(menu_frame_users, text="View Profile").grid(column=0, row=1, sticky='we')
invoices_button = tk.Button(menu_frame_users, text="Invoices").grid(column=0, row=2, sticky='we')
bookings_button = tk.Button(menu_frame_users, text="View bookings").grid(column=0, row=3, sticky='we')
tools_button = tk.Button(menu_frame_users, text="Search tools").grid(column=0, row=4, sticky='we')

test_label = tk.Label(content_frame, text='View profile')
test_label.grid(column=0, row=0, sticky='we', padx=20, pady=20)


menu_frame_users.grid(column=0, row=0, sticky='nswe')
content_frame.grid(column=1, row=0, sticky='nswe')

app.mainloop()

tkinter 的文檔有點有限,但此頁面上有一些關於網格配置的重要信息

一般來說,給定容器中的小部件是容器的尺寸,除非另有明確編碼。 (換句話說,框架會隨着您向其中添加內容而增長,而不是相反)

在您的示例中,我添加了任意窗口大小(您還可以在該字符串參數中指定偏移量)。 我的猜測是您正在尋找rowconfigure()columnconfigure() 此外,您可以添加一些填充以使用.grid()

我幾乎只使用網格幾何管理器,但有時您可能會發現使用pack()place()更實用,只要確保不要同時使用兩者。

干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM