'Display multiple print lines from another function into multiline using PySimpleGui
I am trying to display all the print lines that occur in a function
def colorselected(val):
if val > 50 :
print("This has breached the threshold")
print("Red")
print("Exit program")
else:
print("All good")
print("Green")
print("Continue")
Layout is
layout = [
[sg.Text("ColorValue"),
sg.Input(size=(8, 1), enable_events=True, key="-COLORIS-"),
sg.Button('Show') ,
sg.Multiline(size=(50,2), key="-coloroutput-")
]
And the loop for the PySimpleGui section
while True:
event, values = window.read()
if event == "Show":
display_output=colorselected(50)
window['-coloroutput-'].print(display_output)
Output in the console shows the print statement but not inside the PySimpleGui Multiline text area. It shows None
Solution 1:[1]
Some issues in your code
- There's value returned by your function
colorselected - Bracket missed in your
layout
Options can be added in Multiline element
reroute_stdout=Trueto print directly to Multilinedo_not_clear=Falseto clear content of Multiline for each event- try/except statement used when convert the content of the Input to float
import PySimpleGUI as sg
def colorselected(val):
if val > 50 :
print("This has breached the threshold")
color = "Red"
print("Red")
print("Exit program")
else:
print("All good")
color = 'Green'
print("Green")
print("Continue")
return color
layout = [
[sg.Text("ColorValue"),
sg.Input(size=(8, 1), key="-COLORIS-"),
sg.Button('Show')],
[sg.Multiline(size=(50,2), key="-coloroutput-")]
]
window = sg.Window('Draw', layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == "Show":
try:
val = float(values["-COLORIS-"])
display_output=colorselected(val)
window['-coloroutput-'].print(display_output)
except ValueError:
window['-coloroutput-'].print("Wrong value")
window.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jason Yang |
