【问题标题】:PySimpleGui: change font, font display, Ubuntu, ugly fontsPySimpleGui:更改字体,字体显示,Ubuntu,丑陋的字体
【发布时间】:2021-04-18 12:51:04
【问题描述】:

在使用 Linux (Ubuntu 20.10) 时,demo app examples for PySimpleGui 似乎以“丑陋”的字体显示。

由于我在提供的演示示例中找不到任何参考,例如default_font = 'Helvetica'),因此这些示例似乎隐含地假定默认字体设置应该已经正确。

为了尝试解决这个问题,我安装了Helvetica 和默认的 Windows 字体,但它仍然显示与网上描述的示例不同。

下面的例子显然不是 Helvetica。

我该如何解决这个问题?

import PySimpleGUI as sg

'''
    App that shows "how fonts work in PySimpleGUI".
'''

layout = [[sg.Text('This is my sample text', size=(20, 1), key='-text-')],
          [sg.CB('Bold', key='-bold-', change_submits=True),
           sg.CB('Italics', key='-italics-', change_submits=True),
           sg.CB('Underline', key='-underline-', change_submits=True)],
          [sg.Slider((6, 50), default_value=12, size=(14, 20),
                     orientation='h', key='-slider-', change_submits=True),
           sg.Text('Font size')],
          [sg.Text('Font string = '), sg.Text('', size=(25, 1), key='-fontstring-')],
          [sg.Button('Exit')]]

window = sg.Window('Font string builder', layout)

text_elem = window['-text-']
while True:     # Event Loop
    event, values = window.read()
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    font_string = 'Helvitica '
    font_string += str(int(values['-slider-']))
    if values['-bold-']:
        font_string += ' bold'
    if values['-italics-']:
        font_string += ' italic'
    if values['-underline-']:
        font_string += ' underline'
    text_elem.update(font=font_string)
    window['-fontstring-'].update('"'+font_string+'"')
    print(event, values)

window.close()

更新:除了下面的答案,如果您使用 Anaconda / conda 环境,这似乎也是一个已知问题。我从我的系统中删除了 Anaconda 并改为运行 pipenv 环境,它工作正常。

当我在代码下方运行并且几乎没有任何字体显示时,我注意到了这一点,同时在“正常”环境中,它与 FontManager 的字体列表匹配。

from tkinter import Tk, font

root = Tk()
font_tuple = font.families()
root.destroy()
for font in font_tuple:
    print(font)

【问题讨论】:

  • 您在代码中拼错了 Helvetica - 我假设不存在的字体名称会自动替换为一些默认字体。

标签: python tkinter pysimplegui


【解决方案1】:

有四种方法可以设置元素的字体

  • 将选项font 添加到元素
font = ("Arial, 11)
sg.Text('This is my sample text', size=(20, 1), key='-text-', font=font)
  • 在元素 sg.Text 的方法 update 中通过选项 font 更新元素。
window['-text-'].update(font=font)
  • sg.Window 中通过选项font 设置默认字体
window = sg.Window('Font string builder', layout, font=font)
  • 布局前在方法sg.set_options中通过选项font设置默认字体
sg.set_options(font=font)
layout = [[sg.Text('This is my sample text', size=(20, 1), key='-text-')],
...

默认字体sg.DEFAULT_FONT 是 ("Helvetica", 11)。 您的系统中可能不存在字体,然后将使用另一种 tkinter 默认字体。

为了确定您的系统中存在什么字体,下面的代码将显示所有字体。

from tkinter import Tk, font

root = Tk()
font_tuple = font.families()
root.destroy()
for font in font_tuple:
    print(font)

【讨论】:

    【解决方案2】:

    此代码列出了您系统上的所有字体,适用于 Pydroid Android 并使用 PySimpleGUI。 从上面修改的答案在我的手机上产生黑屏。

    from tkinter import Tk, font
    import  PySimpleGUI as sg
    root = Tk()
    font_tuple = font.families()
    root.destroy()
    #Creates a Empty list to hold font names
    FontList=[]
    for font in font_tuple:
        FontList.append(font)
    #size 28, 28 is optimized for my Android phone please tweak as per your screen
    #Scrolled popup to accommodate big list
    sg.popup_scrolled(FontList, title='All fonts installed using PySimpleGUI', size=(28,28), grab_anywhere=True)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-17
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      相关资源
      最近更新 更多