用PySimpleGUI中的’sg.Text()’使单词中的一个字母加粗。

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

Bold a single letter in a word with 'sg.Text()' in PySimpleGUI

问题

在PySimpleGUI中,我怎样可以只加粗一个单词中的一个字母,比如Hello,使用sg.Text()

当我使用[sg.Text("H", font=("bold")), sg.Text("ello").]时,我只能将整个单词或在“H”和“ello”之间加粗。谢谢。

英文:

On PySimpleGUI, how can I bold only a letter in a word with sg.Text(), like Hello?

I can only bold the entire word or with space between "H" and "ello" when I do [sg.Text("H", font=("bold")), sg.Text("ello").] Thanks.

答案1

得分: 1

Credit to Jason Yang (in comments); here is how you can use a Multiline element to bold a particular part of your text:

import PySimpleGUI as sg

# 注意布局包含一个具有键来引用它的多行元素,并且已禁用以防止用户编辑文本
layout = [
    [sg.Multiline("", key='-VARIEDTEXT-', disabled=True)]
]

window = sg.Window("Test", layout, finalize=True)

# 使用.print函数添加指定字体类型的文本段
window['-VARIEDTEXT-'].print("H", font='bold', end='')
window['-VARIEDTEXT-'].print("ello", end='')

# 这只是常规的窗口设置
while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

为了使.print函数正常工作,必须将窗口设置为finalize=True
此外,默认情况下,end='\n'会创建一个新行。这就是为什么您必须设置end=''以保持在同一行上的原因。

英文:

Credit to Jason Yang (in comments); here is how you can use a Multiline element to bold a particular part of your text:

import PySimpleGUI as sg

# Note that the layout includes a Multiline element with a key to reference it, and it is disabled to keep users from editing the text
layout = [
    [sg.Multiline("", key='-VARIEDTEXT-', disabled=True)]
]

window = sg.Window("Test", layout, finalize=True)

# Use the .print function to add segments of text with specified font types
window["-VARIEDTEXT-"].print("H", font='bold', end='')
window["-VARIEDTEXT-"].print("ello", end='')

# This is just the regular window settings
while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

In order for the .print function to work, you must set the window to finalize=True.
Also, by default, end='\n' which will create a new line. This is why you must set end='' to keep it all on the same line.

huangapple
  • 本文由 发表于 2023年6月1日 08:52:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378055.html
匿名

发表评论

匿名网友

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

确定