简体   繁体   English

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

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

I'm programming a little game with tkinter and briefly, I'm stuck.我正在用 tkinter 编写一个小游戏,简单地说,我被卡住了。

I have a kind od starting menu, in which are two buttons and one label.我有一个很好的起始菜单,其中有两个按钮和一个标签。

If I just create the frame everything is fine, it has the size 500x500 pixels如果我只是创建框架一切都很好,它的大小为 500x500 像素

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()

I've searched around on stackoverflow and didn't get anything useful!我已经在 stackoverflow 上搜索过,但没有得到任何有用的信息! 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.我无法想象这是什么问题,我真的很绝望。

You turn off pack_propagate by setting pack_propagate(0)您可以通过设置pack_propagate(0)来关闭pack_propagate

Turning off pack_propagate here basically says don't let the widgets inside the frame control it's size.在这里关闭pack_propagate基本上是说不要让框架内的小部件控制它的大小。 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所以你已经将它的宽度和高度设置为 500。关闭传播静止允许它是这个大小,而小部件不会改变框架的大小来填充它们各自的宽度/高度,这是正常情况下会发生的

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.要关闭调整根窗口的大小,您可以设置root.resizable(0, 0) ,其中分别允许在xy方向上调整大小。

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.要将 maxsize 设置为 window,如另一个答案中所述,您可以设置maxsize属性或minsize尽管您可以只设置根窗口的几何形状,然后关闭调整大小。 A bit more flexible imo.更灵活一点。

Whenever you set grid or pack on a widget it will return None .每当您在小部件上设置gridpack ,它都会返回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.因此,如果您希望能够保留对小部件对象的引用,则不应将变量设置为正在调用gridpack的小部件。 You should instead set the variable to be the widget Widget(master, ....) and then call pack or grid on the widget instead.您应该将变量设置为小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()

If you want a window as a whole to have a specific size, you can just give it the size you want with the geometry command.如果您希望整个窗口具有特定大小,则可以使用geometry命令为其指定所需大小。 That's really all you need to do.这就是你真正需要做的。

For example:例如:

mw.geometry("500x500")

Though, you'll also want to make sure that the widgets inside the window resize properly, so change how you add the frame to this:但是,您还需要确保窗口内的小部件正确调整大小,因此更改添加框架的方式:

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

Try parent_window.maxsize(x,x);试试parent_window.maxsize(x,x); to set the maximum size.设置最大尺寸。 It shouldn't get larger even if you set the background, etc.即使您设置了背景等,它也不应该变大。

Edit: use parent_window.minsize(x,x) also to set it to a constant size!编辑:使用parent_window.minsize(x,x)也将其设置为恒定大小!

There are 2 solutions for your problem:您的问题有两种解决方案:

  1. Either you set a fixed size of the Tkinter window;要么设置 Tkinter 窗口的固定大小; mw.geometry('500x500')

OR

  1. Make the Frame adjust to the size of the window automatically;使Frame自动适应窗口大小; back.place(x = 0, y = 0, relwidth = 1, relheight = 1)

*The second option should be used in place of back.pack() *应使用第二个选项代替back.pack()

Here is the most simple way.这里是最简单的方法。

import tkinter as tk

root = tk.Tk()

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

root.mainloop()

I don't think there is anything to specify.我不认为有什么要说明的。 It's pretty straight forward.这很直接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 tkinter 大小的文本设置为主要 window 的大小? - How do I set the Text in tkinter size to the size of the main window? 如何使用 ```import Tkinter``` 而不是 ```from tkinter import *``` 设置 Tkinter window 的大小? - How can I set a size for a Tkinter window using ```import Tkinter``` instead of ```from tkinter import *```? 如何动态设置我的小部件的大小和填充以适应 Tkinter 中的 window? - How to set my widgets' size and padding dynamically to fit window in Tkinter? 如何动态设置我的小部件的大小以适应 Tkinter 中的 window? - How to set my widgets' size dynamically to fit window in Tkinter? 不断的Tkinter窗口更新 - Constant Tkinter window update 为小部件设置恒定大小并对齐小部件内的文本,tkinter - set constant size for widget and align text inside the widget, tkinter 如何在 Tkinter 中访问超出窗口大小的条目 - How to access entries beyond the window size in Tkinter 如何仅指定位置而不指定tkinter窗口的大小? - How to specify only the place but not the size of a tkinter window? 如何在Python中更改Tkinter主窗口的大小? - How to change the size of main Tkinter window in Python? 如何将根窗口大小设置为屏幕宽度和高度的一半Python Tkinter - How to set the root window size to half of the screen width and height Python Tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM