Tkinter. 调整字体大小以适应窗口,通过更改窗口大小

huangapple go评论98阅读模式
英文:

Tkinter. Resize the font size to fit in the window via changing window size

问题

我试图创建一个函数,它将根据窗口大小更改按钮字体大小以适应按钮的文本。我尝试使用“步进”函数,其中我使用“像素步进”。像这样:

if 100 < window_width <= 200:
    button.config(font=('Arial', 10))
elif 200 < window_width <= 300:
    button.config(font=('Arial', 20))

但这里有一个问题。如果将过长的文本插入按钮,它将超出“if”条件(因为文本长度与字体大小相对于像素的增长呈指数关系。当然,我可以尝试选择正确的字体值,以使大小的过渡尽可能平滑,但在我看来,这将是非常低效的。此外,这种方法极大地依赖于个人电脑。我不想编写大约30个“if”条件来覆盖从全高清到4K的不同显示器变化。

因此,总结上述所有内容,我需要一种方法,根据屏幕大小,使字体(可选:平滑)自动调整大小,以填充窗口边界内的所有按钮文本。

英文:

I'm trying to create a function, which will change the button font size to fit button's text in the window. I tried to play with 'step' function, where i used 'pixel steps'. Like

if 100 &lt; window_width &lt;= 200: 
     button.config(font=(&#39;Arial&#39;, 10)
elif 200 &lt; window_width &lt;= 300:
     button.config(font=(&#39;Arial&#39;, 20)

But here is a problem. If you insert too long text into the button, it ll run out of the 'if' conditions (as long as text length in pixel grows exponentially relative to the size of the font. Of cource i I can try to choose the right font values to make the transitions in size as smooth as possible, but it ll be really unproductive, imho. Plus this method is extremely PC depended. I don't want to write aka 30 'if' conditions to cover monitor variations from full HD to 4k.

So, summing up all of the above, i need a method, which will cause font (optionally: smooth) resizing, depending on screen size, to fill all button's text in the windows borders.

答案1

得分: 2

感谢每个尝试回答的人。最后,我找到了解决方案。感谢ChatGPT,哈哈。这是我的代码修正。也许对某人有用。这不是一个完美的解决方案,但在我的情况下是最佳之一,不会导致太多的延迟、故障,并且始终适应窗口内的文本。

from tkinter import Button, Tk
root = Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.minsize(width=screen_width // 3, height=screen_height // 3)

button = Button(root, text="myanimelist.net")
button.pack(expand=True)

font_size = 12

def resize_font(event=None):
    global font_size

    width = root.winfo_width()
    height = root.winfo_height()

    a = int(height / 2)
    buff = len(button["text"])
    b = int(width / (buff if buff >= 7 else 7))
    new_font_size = min(a, b)
    if new_font_size != font_size:
        font_size = new_font_size
        button.config(font=("Arial", font_size))

button.bind("<Configure>", resize_font)

root.mainloop()
英文:

Thank you everyone, who tried to answer. At the end i've found the solution. Thank's to ChatGPT, lmao. Here is the code with my correction. May be it ll be usefull for someone. That's not a perfect solution, but one of the best in my case, which doesn't cause too much lag, glithces and fit the text inside the window all the time.

from tkinter import Button, Tk
root = Tk()

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.minsize(width=screen_width // 3, height=screen_height // 3)

button = Button(root, text=&quot;myanimelist.net&quot;)
button.pack(expand=True)

font_size = 12


def resize_font(event=None):
    global font_size

    width = root.winfo_width()
    height = root.winfo_height()

    a = int(height / 2)
    buff = len(button[&quot;text&quot;])
    b = int(width / (buff if buff &gt;= 7 else 7))
    new_font_size = min(a, b)
    if new_font_size != font_size:
        font_size = new_font_size
        button.config(font=(&quot;Arial&quot;, font_size))


button.bind(&quot;&lt;Configure&gt;&quot;, resize_font)

root.mainloop()

huangapple
  • 本文由 发表于 2023年3月4日 06:22:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定