如何在 tkinter 中设置字体大小以适合框架的特定宽度

发布于 2025-01-19 10:14:56 字数 173 浏览 2 评论 0原文

实际上,我正在python的Tkinter的帮助放入。我希望我只是给出一个字符串,它将根据宽度自动调整字体的大小。 例如: - 让我提供一根“ Raj Kundra酒店和家庭餐厅”的字符串,让框架/标签的宽度为500。 因此,它将如何自动在这种窗口的大小上自动调整,而不会包装文本。只需适合窗口尺寸

actually I am making a project with the help of tkinter in python so basically I want to shrink my font size of a label according to the width of the frame that it will be put in. I want that just i am giving a string and it will automatically adjust the size of the font according to the width.
For an Example:- Let I am Giving a String that is " Hotel Raj Kundra and Family Resturant", let the width of the Frame/label is 500.
so how it will automatically adjust in this size of window without wrapped the text. just fit in the window size

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤城病女 2025-01-26 10:14:56

使用 .config,您可以在放置后调整属性。
现在您可以使用字符串的 len() 调整窗口大小。
我希望它有帮助。

import tkinter as tk

def adjust_Window(canvas, text):

    canvas.config(width=len(text)*50)

def main():
    root = tk.Tk()

    canvas = tk.Canvas(root, width=500, height=500)
    canvas.pack()

    adjust_Window(canvas, "i hope it helps <3")

    root.mainloop()


if __name__ == '__main__':
    main()

with .config you can adjust Attributes after you placed it.
Now you can adjust the window size with the len() of your string.
I hope it helps.

import tkinter as tk

def adjust_Window(canvas, text):

    canvas.config(width=len(text)*50)

def main():
    root = tk.Tk()

    canvas = tk.Canvas(root, width=500, height=500)
    canvas.pack()

    adjust_Window(canvas, "i hope it helps <3")

    root.mainloop()


if __name__ == '__main__':
    main()
淡忘如思 2025-01-26 10:14:56

更简单的方法:

from tkinter import *
from tkinter import ttk

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

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

def update_width():
   l.configure(text='Hotel Raj Kundra and Family Resturant', background='blue', foreground='white', font=("Calibri,8,itlaic"))
   

# Create a frame
frame=Frame(win, background="skyblue3", width=700, height=250)
frame.pack()

# Add a button in the main window
ttk.Button(win, text="Update", command=update_width).pack()
l = ttk.Label(win, background='red', text="so how it will automatically adjust in this size of window without wrapped the text. just fit in the window siz" , font=("Calibri,32,Bold"))
l.pack()
          
win.mainloop()

结果最宽:

在此处输入图像描述

缩小结果:

在此处输入图像描述

Easier way to do this:

from tkinter import *
from tkinter import ttk

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

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

def update_width():
   l.configure(text='Hotel Raj Kundra and Family Resturant', background='blue', foreground='white', font=("Calibri,8,itlaic"))
   

# Create a frame
frame=Frame(win, background="skyblue3", width=700, height=250)
frame.pack()

# Add a button in the main window
ttk.Button(win, text="Update", command=update_width).pack()
l = ttk.Label(win, background='red', text="so how it will automatically adjust in this size of window without wrapped the text. just fit in the window siz" , font=("Calibri,32,Bold"))
l.pack()
          
win.mainloop()

Result widest:

enter image description here

Result to shrink:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文