如何在 Tkinter 中设置 Label 控件的高度/宽度?


Label 控件用于在应用程序中显示文本和图像。标签控件的大小取决于多种因素,如标签文本的宽度、高度和字体大小。

高度和宽度定义了标签控件如何在窗口中显示。若要设置标签控件的高度和宽度,我们应该用变量来声明 Label 控件。使用变量实例化标签控件允许用户添加/编辑标签控件的属性。

示例

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win=Tk()

# Set the size of the window
win.geometry("700x350")

# Add a Label widget
label=Label(win, text="How to set the height/width " "of a Label widget in Tkinter?", font=('Times 14'), width=60, height=15)
label.pack()

win.mainloop()

输出

运行上面的代码将显示一个窗口,其中包含一个在宽度和高度内的标签控件。

更新日期: 19-六月-2021

21K+ 次查看

开启您的 职业

完成课程后获得认证

开始
<a href="/articles/index.php"> <button class="button button--neutral"> <svg xmlns="http://www.w3.org/2000/svg" width="10" height="16" viewBox="0 0 10 16" fill="none"><path d="M1.03117 8.48836C0.64065 8.09783 0.64065 7.46467 1.03117 7.07414L7.39514 0.710183C7.78566 0.319658 8.41883 0.319658 8.80935 0.710183C9.19987 1.10071 9.19987 1.73387 8.80935 2.1244L3.15249 7.78125L8.80935 13.4381C9.19987 13.8286 9.19987 14.4618 8.80935 14.8523C8.41882 15.2428 7.78566 15.2428 7.39513 14.8523L1.03117 8.48836ZM3.12109 8.78125L1.73828 8.78125L1.73828 6.78125L3.12109 6.78125L3.12109 8.78125Z" fill="black"></path></svg> 上一页 </button> </a> <a href="https://tutorialspoint.com/how-to-set-the-width-of-a-tkinter-entry-widget-in-pixels"> <button class="button ">下一页 <svg xmlns="http://www.w3.org/2000/svg" width="10" height="16" viewBox="0 0 10 16" fill="none"><path d="M8.87117 8.48836C9.26169 8.09783 9.26169 7.46467 8.87117 7.07414L2.50721 0.710183C2.11668 0.319658 1.48352 0.319658 1.09299 0.710183C0.70247 1.10071 0.70247 1.73387 1.09299 2.1244L6.74985 7.78125L1.093 13.4381C0.702471 13.8286 0.702471 14.4618 1.093 14.8523C1.48352 15.2428 2.11668 15.2428 2.50721 14.8523L8.87117 8.48836ZM6.78125 8.78125L8.16406 8.78125L8.16406 6.78125L6.78125 6.78125L6.78125 8.78125Z" fill="white"></path></svg> </button> </a>
广告