栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

您可以

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660862.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号