【问题标题】:Determine string width in PySimpleGUI在 PySimpleGUI 中确定字符串宽度
【发布时间】:2020-07-30 14:53:44
【问题描述】:

我想创建一个动态屏幕布局。

有没有办法确定特定字体的字符串的宽度?

【问题讨论】:

    标签: python pysimplegui


    【解决方案1】:

    您可以使用get_size() 返回元素的宽度和长度(以像素为单位)。有关更多信息,请查看 PySimpleGUI documentation

    import PySimpleGUI as sg
    
    my_string = 'Hello World!'
    
    layout = [
        [sg.Text(text=my_string, key='-TEXT-', font=('Helvetica', 11))],
        [sg.Button('Go')]]
    
    window = sg.Window(title='Name', layout=layout).finalize()
    
    while True:
        event, values = window.read()
        if event == sg.WIN_CLOSED:
            break
        if event == 'Go':
            # GETTING SIZE OF THE TEXT ELEMENT IN PIXELS
            size_of_text = window['-TEXT-'].get_size()   
    
            print('Width:', size_of_text[0], 'pixels')
            print('Height:', size_of_text[1], 'pixels')        
    
    window.close()
    

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-16
      相关资源
      最近更新 更多