如何将tkinter窗口设置为固定大小

31

我正在使用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)

我无法想象问题出在哪里,我真的很绝望。


4
顺便提一下,不要像那样将 .pack 链接到小部件构造函数上! .pack 返回 None,因此您正在将 None 分配给 gocloseinfo。先定义小部件,然后再将它们打包,就像您在 back 上做的那样。参见 https://dev59.com/cnzaa4cB1Zd3GeqPMSu3 - PM 2Ring
6个回答

54

通过设置pack_propagate(0)来关闭pack_propagate

在这里关闭pack_propagate基本上是告诉框架内的小部件不要控制它的大小。所以您将其宽度和高度设置为500。关闭传播仍然允许它保持这个大小,而不会使小部件更改框架的大小以填充它们各自的宽度/高度,这通常会发生。

要关闭根窗口的调整大小功能,可以设置root.resizable(0,0),分别允许在xy方向上调整大小。

如其他答案中所述,要设置窗口的最大尺寸,可以设置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()

12
如果您希望整个窗口具有特定的大小,只需使用geometry命令指定所需大小即可。这就是您需要做的全部。
例如:
mw.geometry("500x500")

当然,您还需要确保窗口内的小部件可以正确地调整大小,因此请将添加框架的方式更改为以下内容:

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

5
尝试使用parent_window.maxsize(x,x);来设置最大尺寸。即使设置了背景等,它也不应该变得更大。
编辑:还可以使用parent_window.minsize(x,x)将其设置为固定大小!

我不希望它比任何东西都要小,除了一个固定的大小。 - Mr. Squiddy
指定root.resizable(0,0)以创建固定大小的窗口的接受答案对我来说不是一个解决方案,因为它有一个副作用(至少在Mac上):使用窗口控制按钮关闭/最小化窗口必须具有焦点(您必须单击窗口,然后单击(x))。但是,设置root.maxsize(a,b)和root.minsize(a,b)可以通过单击完成。非常感谢@SidSahay - Mikhail Zakharov

4
这里是最简单的方法。
import tkinter as tk

root = tk.Tk()

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

root.mainloop()

我认为没有什么需要特别说明的。 这很简单明了。


2

您的问题有两种解决方案:

  1. 设置Tkinter窗口的固定大小:mw.geometry('500x500')

或者

  1. 使帧(Frame)自动调整到窗口的大小:back.place(x=0, y=0, relwidth=1, relheight=1)

*第二个选项应该用来替代back.pack()


1
from tkinter import *

root = Tk()

width = root.winfo_screenwidth() #get your Windows width size 
height = root.winfo_screenheight() #get your Windows height size 

root.geometry("%dx%d" % (width, height))
root.resizable(False,False)

root.mainloop()

#full size Tkinter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接