I'm working on UI for a virus simulation me and a friend are making and I'm really struggling to change the background color of the UI. I took most of the code from the one of the demo projects because i couldn't figure out how to implement matplotlib with pysimplegui so there's some things I don't fully understand but usually with pysimplegui it's as simple as sg.theme="color
to change the main background color but it isn't working this time. Any help would be really appreciated, thanks.
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()
CodePudding user response:
You can change the background color by specifying a Hex Color Code in the following argument under layout
:
background_color='#DAE0E6'
You can use a Color Picker like this one https://htmlcolorcodes.com/color-picker/ to get your color
You can also use:
window = sg.Window('Virus Simulation', layout, background_color='hex_color_code')
To change the color of a window object
CodePudding user response:
I'm a tad confused in 2 ways.
1 - I don't see a call to sg.theme()
in your code, so I don't know where it was in the code. WHERE it is placed matters. Always place the theme as early as possible, definitely before making your layout.
2 - I don't know what "it isn't working this time means". Again, it's more of needing to see a complete example to get it.
The sample code in the question that you said normally works was weirdly formatted so something must have been scrambled.
The question shows: sg.theme="color
But as Jason has pointed out, the value passed to sg.theme()
is more than a color. The "Theme Name Formula" is described in the main PySimpleGUI documentation here - https://pysimplegui.readthedocs.io/en/latest/#theme-name-formula. In case there's a problem getting to that section, here's what it says:
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.
If you want to only change the background color of your theme, then you can use individual color names or hex values. sg.theme_background_color('#FF0000')
or sg.theme_background_color('red')
will set the background color to red.
Hope that helps with themes.
Nice work on using the PSG coding conventions. Looking at your code was effortless as a result. Zero guesswork as to what I was seeing. Great to see and it helps in numerous ways when you use them.