如何在 tkinter 中设置小部件的大小? [英] how to set a widget's size in tkinter?

查看:62
本文介绍了如何在 tkinter 中设置小部件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单例子:

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:

  1. 4060 到底是什么意思?

  1. 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屋!

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