簡體   English   中英

如何將 tkinter 窗口設置為恆定大小

[英]How to set a tkinter window to a constant size

我正在用 tkinter 編寫一個小游戲,簡單地說,我被卡住了。

我有一個很好的起始菜單,其中有兩個按鈕和一個標簽。

如果我只是創建框架一切都很好,它的大小為 500x500 像素

我希望在創建按鈕和標簽時​​背景不會改變,但無論我做什么,它都會適應大小。 這是我的代碼:

import tkinter as tk

def startgame():
    pass

mw = tk.Tk()              #Here I tried (1)
mw.title('The game')

back = tk.Frame(master=mw, width=500, height=500, bg='black')
back.pack()

go = tk.Button(master=back, text='Start Game', bg='black', fg='red',
                     command=lambda:startgame()).pack()
close = tk.Button(master=back, text='Quit', bg='black', fg='red',
                     command=lambda:quit()).pack()
info = tk.Label(master=back, text='Made by me!', bg='red',
                         fg='black').pack()

mw.mainloop()

我已經在 stackoverflow 上搜索過,但沒有得到任何有用的信息! 我發現只有一個問題與我的有點相似,但答案不起作用。 我試過這個:

(1) mw.resizable(width=False, height=False)

我無法想象這是什么問題,我真的很絕望。

您可以通過設置pack_propagate(0)來關閉pack_propagate

在這里關閉pack_propagate基本上是說不要讓框架內的小部件控制它的大小。 所以你已經將它的寬度和高度設置為 500。關閉傳播靜止允許它是這個大小,而小部件不會改變框架的大小來填充它們各自的寬度/高度,這是正常情況下會發生的

要關閉調整根窗口的大小,您可以設置root.resizable(0, 0) ,其中分別允許在xy方向上調整大小。

要將 maxsize 設置為 window,如另一個答案中所述,您可以設置maxsize屬性或minsize盡管您可以只設置根窗口的幾何形狀,然后關閉調整大小。 更靈活一點。

每當您在小部件上設置gridpack ,它都會返回None 因此,如果您希望能夠保留對小部件對象的引用,則不應將變量設置為正在調用gridpack的小部件。 您應該將變量設置為小Widget(master, ....) ,然后在小部件上調用packgrid

import tkinter as tk

def startgame():

    pass

mw = tk.Tk()

#If you have a large number of widgets, like it looks like you will for your
#game you can specify the attributes for all widgets simply like this.
mw.option_add("*Button.Background", "black")
mw.option_add("*Button.Foreground", "red")

mw.title('The game')
#You can set the geometry attribute to change the root windows size
mw.geometry("500x500") #You want the size of the app to be 500x500
mw.resizable(0, 0) #Don't allow resizing in the x or y direction

back = tk.Frame(master=mw,bg='black')
back.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
back.pack(fill=tk.BOTH, expand=1) #Expand the frame to fill the root window

#Changed variables so you don't have these set to None from .pack()
go = tk.Button(master=back, text='Start Game', command=startgame)
go.pack()
close = tk.Button(master=back, text='Quit', command=mw.destroy)
close.pack()
info = tk.Label(master=back, text='Made by me!', bg='red', fg='black')
info.pack()

mw.mainloop()

如果您希望整個窗口具有特定大小,則可以使用geometry命令為其指定所需大小。 這就是你真正需要做的。

例如:

mw.geometry("500x500")

但是,您還需要確保窗口內的小部件正確調整大小,因此更改添加框架的方式:

back.pack(fill="both", expand=True)

試試parent_window.maxsize(x,x); 設置最大尺寸。 即使您設置了背景等,它也不應該變大。

編輯:使用parent_window.minsize(x,x)也將其設置為恆定大小!

您的問題有兩種解決方案:

  1. 要么設置 Tkinter 窗口的固定大小; mw.geometry('500x500')

  1. 使Frame自動適應窗口大小; back.place(x = 0, y = 0, relwidth = 1, relheight = 1)

*應使用第二個選項代替back.pack()

這里是最簡單的方法。

import tkinter as tk

root = tk.Tk()

root.geometry('200x200')
root.resizable(width=0, height=0)

root.mainloop()

我不認為有什么要說明的。 這很直接。

暫無
暫無

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

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