如何在 tkinter 中设置小部件的大小?

编程入门 行业动态 更新时间:2024-10-27 00:26:11
本文介绍了如何在 tkinter 中设置小部件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

简单例子:

from tkinter import *

class GUI_CMP():
    def __init__(self):
        self.tk = Tk()
        self.text = Text(self.tk,width=60,height=40)
        self.text.pack()

        self.tk.mainloop()

if __name__ == '__main__':
    gui_cmp = GUI_CMP()

这是它的样子:

如您所见,虽然我设置了 width=60,height=40,但文本小部件的宽度小于其高度.每次使用 tkinter 时,这一直困扰着我.所以我的问题是:

As you can see,though I set width=60,height=40,the width of text widget is smaller than its height.This keeps disturbing me every time I use tkinter.So my questions are:

4060 到底是什么意思?

What exactly does 40 and 60 mean?

文本宽度小于高度的原因是什么?

What is the reason that text's width is smaller than its height?

控制尺寸的最佳方法是什么?

What is the best way to do size controll?

推荐答案

当您指定宽度和高度时,它们不是以像素为单位;它们根据当前字体大小以字符和行来衡量

when you specify the width and height they aren't in pixels; they are measured by characters and lines depending on the current font size

这就是为什么当您执行 Text(self.tk,width=60,height=40) 时,60 表示文本小部件的宽度为 60 个字符,而 40 表示其高度为 40 行

that's why when you do Text(self.tk,width=60,height=40) the 60 means the text widget is 60 characters wide and the 40 means its 40 lines high

这也适用于按钮

这就是混淆的来源,因为它不是以像素为单位,如果你改变字体,它会改变文本小部件的大小!

that's where the confusion comes from, because its not in pixels and if you change the font it ill change the size of the text widget!

这可能是控制尺寸的最佳方法,起初只是令人困惑,但现在您知道它会更有意义!

and that's probably the best way to do size control its just confusing at first but now that you know it will make more sense!

看看这个网站 Text Widget Info 并查看参数更多信息

take a look at this site Text Widget Info and look through the parameters for more information

这篇关于如何在 tkinter 中设置小部件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 07:26:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:部件   大小   如何在   tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!