【问题标题】:Select a single row of a list box in PySimpleGUI在 PySimpleGUI 中选择列表框的单行
【发布时间】:2021-02-21 03:44:38
【问题描述】:

我的问题很简单,我是 PySimpleGUI 的初学者,我想知道如何更改列表框中文本的颜色,但我只想更改一些特定的行,所以我可以运行所有列表并选择行。 有人知道怎么做,我会非常感激的。

【问题讨论】:

  • 您可以使用text_color 更改整个列表框的文本颜色。如果不进行更深层次的更改,就无法更改单个行(例如 tk,但这会因操作系统而异,所以这不是微不足道的。)
  • 多行打印是否可行?

标签: python listbox pysimplegui


【解决方案1】:

为列表框中的项目设置选项所需的 tkinter 代码。

import PySimpleGUI as sg

sg.theme("DarkBlue")

items = ['USA', 'Mexico', 'Japan', 'Korea', 'UK', 'China', 'France']
asia_index = (2 ,3, 5)

layout = [
    [sg.Listbox(items, size=(10, 7), key='-LISTBOX-')],
]
window = sg.Window('Title', layout, finalize=True)
listbox = window['-LISTBOX-'].Widget
for index in asia_index:
    listbox.itemconfigure(index, bg='green', fg='white')    # set options for item in listbox
while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    print(event, values)

window.close()

【讨论】:

    【解决方案2】:

    您可以通过在创建文本时添加 text_color='COLOR' 来更改文本的颜色。

    例如:

    Sg.Text("My text", key="sub_title", size=(15, 1), text_color='yellow')
    

    如果您想更改按钮的颜色,您需要使用 button_color=('FIRST_COLOR', 'SECOND_COLOR'),如下所示:

        Sg.Button("Update", key='update_button', size=(25, 1), button_color=('blue', 'purple'))
    

    享受吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 2020-01-08
      相关资源
      最近更新 更多