MCPcopy
hub / github.com/PySimpleGUI/PySimpleGUI / main

Function main

DemoPrograms/Demo_Button_Toggle2.py:43–104  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41
42# Main function that creates the layout, window and has event loop
43def main():
44 layout = [[sg.Text('Toggle Button')],
45 [sg.T('Disabled with PySimpleGUI Ignore:', text_color='yellow')],
46 [sg.Button(image_data=on_image, k='-TOGGLE1-', border_width=0,
47 button_color=(sg.theme_background_color(), sg.theme_background_color()),
48 disabled_button_color=(sg.theme_background_color(), sg.theme_background_color()),
49 metadata=BtnInfo()),
50 sg.T('Disable:'),
51 sg.Button(image_data=off_image, k='-DISABLE1-', border_width=0,
52 button_color=(sg.theme_background_color(), sg.theme_background_color()),
53 disabled_button_color=(sg.theme_background_color(), sg.theme_background_color()),
54 metadata=BtnInfo(False)), sg.T('Disabled button color is\nbetter than other disabled button below')
55 ],
56 [sg.Button(image_data=on_image, k='-TOGGLE2-', border_width=0,
57 button_color=(sg.theme_background_color(), sg.theme_background_color()),
58 disabled_button_color=(sg.theme_background_color(), sg.theme_background_color()),
59 metadata=BtnInfo()),
60 sg.Image(data=sg.EMOJI_BASE64_HAPPY_THUMBS_UP,enable_events=True, k='-I-')
61 ],
62 [ sg.T('Disabled with GUI:', text_color='yellow')],
63 [sg.Button(image_data=on_image, k='-TOGGLE3-', border_width=0,
64 button_color=(sg.theme_background_color(), sg.theme_background_color()),
65 disabled_button_color=(sg.theme_background_color(), sg.theme_background_color()),
66 disabled=True, metadata=BtnInfo()), sg.T('Note color has crosshatching')],
67 [ sg.T('Disabled with PySimpleGUI (ignored):', text_color='yellow')],
68 [sg.Button(image_data=on_image, k='-TOGGLE4-', border_width=0,
69 button_color=(sg.theme_background_color(), sg.theme_background_color()),
70 disabled_button_color=(sg.theme_background_color(), sg.theme_background_color()),
71 disabled=sg.BUTTON_DISABLED_MEANS_IGNORE,
72 metadata=BtnInfo())],
73 [sg.T(size=(40,1), k='-STATUS-')],
74 [sg.Button('Exit')]]
75
76 window = sg.Window('Window Title', layout, font='_ 14', finalize=True)
77
78 while True: # Event Loop
79 event, values = window.read()
80 if event in (sg.WIN_CLOSED, 'Exit'):
81 break
82 # Where all the magic happens. 2 things happen when button is clicked
83 # 1. The state toggles
84 # 2. The buton graphic changes
85 if 'TOGGLE' in event:
86 window[event].metadata.state = not window[event].metadata.state
87 window[event].update(image_data=on_image if window[event].metadata.state else off_image)
88 elif event == '-DISABLE1-':
89 window[event].metadata.state = not window[event].metadata.state
90 window[event].update(image_data=on_image if window[event].metadata.state else off_image)
91 window['-I-'].update(data=sg.EMOJI_BASE64_HAPPY_GASP if window[event].metadata.state else random.choice(sg.EMOJI_BASE64_HAPPY_LIST))
92 # if disabling the button
93 if window[event].metadata.state:
94 if window['-TOGGLE1-'].metadata.state is True:
95 window['-TOGGLE1-'].update(disabled=sg.BUTTON_DISABLED_MEANS_IGNORE, image_data=on_image_disabled)
96 elif window['-TOGGLE1-'].metadata.state is False:
97 window['-TOGGLE1-'].update(disabled=sg.BUTTON_DISABLED_MEANS_IGNORE, image_data=off_image_disabled)
98 else:
99 if window['-TOGGLE1-'].metadata.state is True:
100 window['-TOGGLE1-'].update(disabled=False, image_data=on_image)

Callers 1

Calls 4

readMethod · 0.95
closeMethod · 0.95
BtnInfoClass · 0.85
updateMethod · 0.45

Tested by

no test coverage detected