【问题标题】:How do I change background color in pysimplegui?如何在 pysimplegui 中更改背景颜色?
【发布时间】:2021-11-08 01:27:04
【问题描述】:

我正在为我和一个朋友制作的病毒模拟 UI 工作,我真的很难改变 UI 的背景颜色。我从一个演示项目中获取了大部分代码,因为我不知道如何使用 pysimplegui 实现 matplotlib,所以有些东西我不完全理解,但通常使用 pysimplegui 它就像sg.theme="color 一样简单更改主要背景颜色,但这次它不起作用。任何帮助将不胜感激,谢谢。

import PySimpleGUI as sg 
import numpy as np
import tkinter 

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk

def draw_figure(canvas, fig):
    if canvas.children:
        for child in canvas.winfo_children():
            child.destroy()
    figure_canvas_agg = FigureCanvasTkAgg(fig, master=canvas)
    figure_canvas_agg.draw()
    figure_canvas_agg.get_tk_widget().pack(side='right', fill='both', expand=1)


# ------------------------------- PySimpleGUI CODE

layout = [
    [sg.Text("Population size: "), sg.Input(key="-POPULATIONSIZE-")],
    [sg.Text("Duration: "), sg.Input(key="-DURATION-")],
    [sg.Text("R Number: "), sg.Input(key="-RNUMBER-")],
    [sg.Text("Starting Infections: "), sg.Input(key="-STARTINGINFECTIONS-")],
    [sg.B('OK'), sg.B('Exit')],
    [sg.Canvas(key='controls_cv')],
    [sg.T('Figure:')],
    [sg.Column(
        layout=[
            [sg.Canvas(key='fig_cv',
                       size=(400 * 2, 400)
                       )]
        ],
        background_color='#DAE0E6',
        pad=(0, 0)
    )],
]

window = sg.Window('Virus Simulation', layout,)

while True:
    event, values = window.read()
    print(event, values)
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    elif event is 'OK':
        # ------------------------------- PASTE YOUR MATPLOTLIB CODE HERE
        plt.figure(1)
        fig = plt.gcf()
        DPI = fig.get_dpi()
        # ------------------------------- you have to play with this size to reduce the movement error when the mouse hovers over the figure, it's close to canvas size
        fig.set_size_inches(404 * 2 / float(DPI), 404 / float(DPI))
        # -------------------------------
        x = list(range(1, 100))
        y = list(range(1, 100))
        plt.plot(x, y, color="r")
        plt.title('Virus Infections Data')
        plt.xlabel('Time in Days')
        plt.ylabel('Infections')
        plt.grid()

        # ------------------------------- Instead of plt.show()
        draw_figure(window['fig_cv'].TKCanvas, fig,)


window.close()

【问题讨论】:

  • 当前的背景颜色是什么?你期望的颜色是什么?
  • 您可以使用sg.theme(theme_name)为您的窗口设置主题,例如sg.theme('LightGreen6'),或者直接在sg.Window中设置选项background_color。您可以通过plt.figure(1, facecolor='green'); ax = plt.axes(); ax.set_facecolor("yellow")为matplotlib设置图形的颜色。
  • 由于某种原因sg.theme(theme_name) 不起作用,这很奇怪,因为它通常与 pysimplegui 一起使用。但是,按照您的建议手动为每个元素单独设置颜色似乎工作正常,唯一的问题是它会花费更多时间并且在尝试快速更改配色方案时可能会很乏味。谢谢你帮助我。

标签: python user-interface tkinter pysimplegui


【解决方案1】:

您可以通过在layout 下的以下参数中指定十六进制颜色代码来更改背景颜色:

background_color='#DAE0E6'

您可以使用像 https://htmlcolorcodes.com/color-picker/ 这样的颜色选择器来获取您的颜色

你也可以使用:

window = sg.Window('Virus Simulation', layout, background_color='hex_color_code')

改变窗口对象的颜色

【讨论】:

  • 这不起作用,但我确实使用window = sg.Window('Virus Simulation', layout, background_color='#000000') 行修复了它,感谢您的帮助。
  • 我已经更新了我的答案,如果它解决了您的问题,请考虑支持并接受我的答案。 (帮助 SO 社区)。
【解决方案2】:

我在两个方面有点困惑。

1 - 我在您的代码中没有看到对sg.theme() 的调用,所以我不知道它在代码中的位置。它放在哪里很重要。始终尽早放置主题,绝对是在进行布局之前。 2 - 我不知道“这次不工作是什么意思”。同样,更多的是需要看到一个完整的例子来获得它。

您所说的正常工作的问题中的示例代码的格式很奇怪,所以一定是打乱了。

问题显示:sg.theme="color

但正如 Jason 所指出的,传递给 sg.theme() 的值不仅仅是一种颜色。 “主题名称公式”在此处的主要 PySimpleGUI 文档中进行了描述 - https://pysimplegui.readthedocs.io/en/latest/#theme-name-formula。如果在进入该部分时遇到问题,它是这样说的:

Theme Name Formula
Themes names that you specify can be "fuzzy". The text does not have to match exactly what you see printed. For example "Dark Blue 3" and "DarkBlue3" and "dark blue 3" all work.

One way to quickly determine the best setting for your window is to simply display your window using a lot of different themes. Add the line of code to set the theme - theme('Dark Green 1'), run your code, see if you like it, if not, change the theme string to 'Dark Green 2' and try again. Repeat until you find something you like.

The "Formula" for the string is:

Dark Color #

or

Light Color #

Color can be Blue, Green, Black, Gray, Purple, Brown, Teal, Red. The # is optional or can be from 1 to XX. Some colors have a lot of choices. There are 13 "Light Brown" choices for example.

如果您只想更改主题的背景颜色,则可以使用单独的颜色名称或十六进制值。 sg.theme_background_color('#FF0000')sg.theme_background_color('red') 会将背景颜色设置为红色。

希望对主题有所帮助。


在使用 PSG 编码约定方面做得很好。结果,查看您的代码毫不费力。我所看到的零猜测。很高兴看到,当您使用它们时,它会在很多方面有所帮助。

【讨论】:

    猜你喜欢
    • 2011-11-11
    • 1970-01-01
    • 2011-07-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2018-02-13
    相关资源
    最近更新 更多