Tkinter 百分比大小? [英] Tkinter percentage sizes?

查看:91
本文介绍了Tkinter 百分比大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Tkinter 模块和 python 构建一个小软件,但我不知道如何使用百分比而不是像素来设置小部件大小.我当然可以通过随时抓取整个窗口的大小并除以 2 得到 50% 的大小来实现,但是有没有更好的方法来使用 Tkinter 模块来实现?

I am trying to build a small software with the Tkinter module and python but I can't figure out how to set a widget size with percentages instead of pixels. I could of course do it by grabbing the size of the entire window at every moment and divide by 2 to get a size of 50%, but is there a better way to do it with the Tkinter module?

编辑 1:为了回答您的问题,我将代码放在这里:

EDIT 1: To answer your questions I put my code here:

class Navbar:
    def __init__(self, master):
        self.maxsizeFrame = Frame(master, height = 50) #maxsize handler, prevent self.frame to grow too big
        self.maxsizeFrame.pack(fill = X, expand = NO)
        self.frame = Frame(self.maxsizeFrame)
        self.frame.pack(side = TOP, fill = BOTH, expand = YES)
        self.masteries = Button(self.frame, text = "Masteries")
        self.masteries.pack(side = LEFT, fill = BOTH, expand = YES)
        self.runes = Button(self.frame, text = "Runes")
        self.runes.pack(side = RIGHT, fill = BOTH, expand = YES)

所以我想创建一个导航栏,其中包含在self.frame"中处理的两个按钮.但我希望这个设计具有响应性,这就是我将扩展设置为是"的原因.但是,我想为self.frame"设置最大尺寸,但我发现的唯一方法是将此框架打包到另一个框架中(self.maxsizeFrame"),并将此框架的 expand 设置为NO".最后,我想将最大扩展大小设置为主窗口的一半,所以如果self.maxsizeframe"的高度可以是百分比,那就太好了.感谢阅读.

So I want to create a navigation bar with two button handled in "self.frame". But I want this design to be responsive, that's why I set expand to "YES". However I wanted to set a maximum size for "self.frame" but the only way I found was to pack this frame in an other one ("self.maxsizeFrame") and set expand to "NO" on this one. And finally, I would like to set the maximum expand size to half the main window, so it could be great if the height of "self.maxsizeframe" could be in percentage. Thanks for reading.

编辑 2:实际上,使用网格布局构建软件似乎更有效,并且权重的选项会准确.

EDIT 2: Actually it seems more efficient to build the software with the grid layout and the weight's option would be accurate.

推荐答案

虽然这取决于小部件,但您应该可以做到 width=30% 并且与高度相同.您能否编辑您的帖子,说明您正在使用哪个小部件.我不推荐百分比,因为它们有时会变得时髦.

Although it depends on the widget, you should be able to do width=30% and same with height. Can you please edit your post saying which widget you are using. I would not recommend percents though as they get funky some times.

这篇关于Tkinter 百分比大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆