问题描述
我正在用 tkinter 编写一个小游戏,简单地说,我卡住了.
I'm programming a little game with tkinter and briefly, I'm stuck.
我有一个很好的开始菜单,里面有两个按钮和一个标签.
I have a kind od starting menu, in which are two buttons and one label.
如果我只是创建框架,一切都很好,它的大小为 500x500 像素
If I just create the frame everything is fine, it has the size 500x500 pixels
我希望在创建按钮和标签时背景不会改变,但无论我做什么,它都会适应大小.这是我的代码:
I want the background not to change when I create the buttons and the labe, but it adapts the size whatever I do. Here is my code:
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 上四处搜索,没有得到任何有用的信息!我发现只有一个问题与我的有点相似,但答案不起作用.我试过这个:
I've searched around on stackoverflow and didn't get anything useful!I've found just one question a bit similar to mine but the answer didn't work. I tried this:
(1) mw.resizable(width=False, height=False)
(1) mw.resizable(width=False, height=False)
我无法想象这是什么问题,我真的很绝望.
I can't imagine what is the problem, I'm really desperate.
推荐答案
您可以通过设置 pack_propagate(0)
关闭 pack_propagate
这里基本上是说不要让框架内的小部件控制它的大小.所以你已经将它的宽度和高度设置为 500.关闭传播静止允许它是这个大小,而小部件不会改变框架的大小以填充它们各自的宽度/高度,这通常会发生
Turning off pack_propagate
here basically says don't let the widgets inside the frame control it's size. So you've set it's width and height to be 500. Turning off propagate stills allows it to be this size without the widgets changing the size of the frame to fill their respective width / heights which is what would happen normally
要关闭调整根窗口大小,可以设置root.resizable(0, 0)
,其中x
和y 方向.
To turn off resizing the root window, you can set root.resizable(0, 0)
, where resizing is allowed in the x
and y
directions respectively.
要将最大大小设置为窗口,如另一个答案中所述,您可以设置 maxsize
属性或 minsize
尽管您可以只设置根窗口的几何形状和然后关闭调整大小.更灵活一点.
To set a maxsize to window, as noted in the other answer you can set the maxsize
attribute or minsize
although you could just set the geometry of the root window and then turn off resizing. A bit more flexible imo.
每当您在小部件上设置 grid
或 pack
时,它都会返回 None
.因此,如果您希望能够保留对小部件对象的引用,则不应将变量设置为正在调用 grid
或 pack
的小部件它.您应该将变量设置为小部件 Widget(master, ....)
然后在小部件上调用 pack
或 grid
.
Whenever you set grid
or pack
on a widget it will return None
. So, if you want to be able to keep a reference to the widget object you shouldn't be setting a variabe to a widget where you're calling grid
or pack
on it. You should instead set the variable to be the widget Widget(master, ....)
and then call pack
or grid
on the widget instead.
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()
这篇关于如何将 tkinter 窗口设置为恒定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!