您可以
pack_propagate通过设置关闭
pack_propagate(0)
pack_propagate基本上,关闭此处表示不要让框架内的小部件控制其大小。因此,您已将其宽度和高度设置为500。关闭传播静止图像时,可以将其设置为该大小,而无需小部件更改帧的大小以填充其各自的宽度/高度,这通常会发生这种情况。
要关闭根窗口的大小调整,您可以设置
root.resizable(0, 0),其中分别在
x和
y方向允许调整大小。
如另一个答案中所述,要将窗口的最大尺寸设置为窗口,可以设置
maxsize属性,或者
minsize可以设置根窗口的几何形状,然后关闭调整大小。imo更加灵活。
无论何时设置
grid或
pack在小部件上,它都会返回
None。因此,如果您希望能够保留对窗口小部件对象的引用,则不应在正在调用
grid或
pack在其上的窗口小部件上设置变量。您应该将变量设置为小部件
Widget(master,....),然后调用
pack或
grid在小部件上。
import tkinter as tkdef startgame(): passmw = 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 sizemw.geometry("500x500") #You want the size of the app to be 500x500mw.resizable(0, 0) #Don't allow resizing in the x or y directionback = tk.frame(master=mw,bg='black')back.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / heightback.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()


