[英]PySimpleGUI - Multiline resize window when I'm updating text font size
我正在使用“sg.Multiline”來顯示一些文本。 我想通過像這樣更新文本字體來即時更改為更大的文本字體:
window['result'].update("some text", text_color='red',font=(10,10))
由於這種方法,window 本身和“多行”元素的大小也發生了變化,變得非常不合理...我想保持 window 和多行元素的原始大小,只顯示更大的文本字體。
提前致謝。
Multiline 元素的大小由字體大小決定,因此如果您更新字體大小,它會發生變化。
插入到小部件中的文本的默認字體。 請注意,通過使用標簽更改某些文本的屬性,您可以在小部件中擁有多個 fonts。
使用標簽配置來改變一些帶有標簽的文本的屬性。 此處需要 tkinter 代碼。
from random import randint
import PySimpleGUI as sg
def random_color():
return '#%02X%02X%02X' % (randint(0,255), randint(0,255), randint(0,255))
def random_size():
return randint(8, 16)
font = ('Courier New', 12)
layout = [
[sg.Multiline('', size=(40, 10), font=font, key='-ML-')],
[sg.Push(), sg.Button('Hello World'), sg.Push()]
]
window = sg.Window('Title', layout, finalize=True)
# Get tkinter widget after window finalized
multiline = window['-ML-'].widget
count = 0
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Hello World':
tag_name = f'Tag {count}'
# Insert text to end of the multiline with tag name
multiline.insert(sg.tk.END, 'Hello World ', (tag_name,))
color, size = random_color(), random_size()
# Configure the attributes for the text with this tag_name
multiline.tag_configure(tag_name, foreground=color, font=('Arial', size))
# Autoscroll to the end of multiline when necessary
multiline.see(sg.tk.END)
count += 1
window.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.