I am using PySimpleGUI to create a text output box.
On opening the program, I would like the Output to display some default text before any button is pressed.
How can I get this to occur? window.Read() waits for a button press. window.refresh() doesn't appear to force the text to the window.
import PySimpleGUI as sg
initialString = "I want this text to display on window opening."
def gui2():
layout = [
[sg.Output(size=(90,20), background_color='black', text_color='white')],
[sg.Button('Do things'), sg.Button('Exit')]
]
window = sg.Window("Funny Title", layout)
#window.read() #I need to press a button before the text will display
#window.refresh() #doesn't refresh the output
print(initialString)
#window.refresh() #doesn't refresh the output
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == 'Do things':
print("You pressed the button")
window.close()
gui2()
and of course, the answer is found 5 minutes after I post.
window = sg.Window("Funny Title", layout, finalize = True)
This fixes the window, and the subsequent print statement appears in the Output as wanted.
.
Edit:
From the comment below, I've also changed the layout from sg.Outline to sg.Multiline:
sg.Multiline(size=(90,20),
background_color='black',
text_color='white',
reroute_stdout=True,
reroute_stderr=True,
autoscroll = True)],
Related
I'm creating a GUI and I had an issue. My GUI is going to interact with the users, so depending on the user's input I want a button to appear. How can I do it? Because so far I can only make the button appear once the window opens.
For example, on the image below I have a sg.InputText isolated, but what I realy want is that this widget appears only after the first sg.InputText is filled.
enter image description here
Create a Button element with option visible=False, update it with visible=True when the value of Input element not empty.
import PySimpleGUI as sg
layout = [
[sg.Image(data=sg.EMOJI_BASE64_HAPPY_JOY),
sg.Text("TRAINING PROGRAM"), sg.Push(),
sg.Button("Check", visible=False)],
[sg.Text("Pass the badge on the reader:"),
sg.Input('', enable_events=True, key='-INPUT-')],
]
window = sg.Window('Title', layout, finalize=True)
shown = False
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == '-INPUT-':
if values[event] != '':
if not shown:
shown = True
window['Check'].update(visible=True)
else:
shown = False
window['Check'].update(visible=False)
window.close()
Hello i have a small program that gets input from user, I suddenly noticed that after few uses in the program I cant use CONTROL + A / CONTROL + C /CONTROL + D the inputText doesnt respond.
if I restart the program this buttons events work.
layout = [[sg.Text('WELCOME', justification='center', size=(50))],
[sg.InputText(), sg.Text(': ENTER SOME TEXT ', justification='right')],
[sg.Button('Search', bind_return_key=True)]
window = sg.Window('Client Connection V2.0Beta2', layout, element_justification='c')
while True:
event, values = window.read()
user_text= values[0]
if event == sg.WIN_CLOSED : # if user closes window or clicks cancel
window.close()
break
elif event == 'Search: # First step user search for
#some Code....
It should be fine for following code
import PySimpleGUI as sg
layout = [
[sg.Text('WELCOME', justification='center', expand_x=True)],
[sg.Text('ENTER SOME TEXT: '), sg.InputText()],
[sg.Button('Search', bind_return_key=True)],
]
window = sg.Window('Client Connection V2.0Beta2', layout, element_justification='c')
while True:
event, values = window.read()
if event == sg.WIN_CLOSED: # if user closes window or clicks cancel
break
elif event == 'Search':
user_text = values[0]
print(user_text)
window.close()
In your description, it didn't show how about the other keyboard input.
It maybe caused by the focus not on the input element, maybe you press the TAB key by accidentally, then the focus shift to next element, 'Search` button here, then all the keyboard input will not be responded in the input element.
In following figure, button Search focused and with a dash box on it, and then the keyboard input to input element will be not responded.
Try to press TAB key to move focus to input element.
I'm working with plots and I want to call a PySimpleGUI window on a press of a TK button which will show my plot tuples and allow modifying them to later re-draw the plot. So far I took this cookbook example and added a save button:
layout += [[sg.Button('Save')]]
This is how I call it:
def readWindow(event):
values = window.read()
print(values)
editButton.on_clicked(readWindow)
and it successfully passes values when I click "save". But if I close the window and try to open it again, I get (None, None) in the console.
You don't need tkinter Button.
import PySimpleGUI as sg
# layout = ...
layout += [[sg.Button("Save", ...]]
window = sg.Window("Title", layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == "Save":
print(event, values)
window.close()
I'm creating a textbox as follows:
sg.Text(size=(57, 10), background_color='white', text_color='red',
key='_console')
And it works fine, except the text isn't selectable!
I want the user to be able to copy the message to clipboard (by mouse selection and "copy").
How can it be done?
thx
According to the git hub issue related to this, the way to go about doing this is to create a read-only input and format it to look like the normal text elements:
https://github.com/PySimpleGUI/PySimpleGUI/issues/2928
import PySimpleGUI as sg
sg.theme('Dark Red')
layout = [ [sg.Text('My Window')],
[sg.InputText('You can select this text', use_readonly_for_disable=True, disabled=True, key='-IN-')],
[sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout, finalize=True)
window['-IN-'].Widget.config(readonlybackground=sg.theme_background_color())
window['-IN-'].Widget.config(borderwidth=0)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
window.close()
I am following the PySimpleGUI documentation and making my own edits as I go along. I am very new to it and have had experience with using Tkinter. There is a Textbox in Tkinter which you an get with the code Text(window, width=?, height=?, wrap=WORD, background=yellow). However in PySimpleGUI with similar code: layout = [[sg.Text('Some text on Row 1')]] - creates a label. My code is:
import PySimpleGUI as sg
sg.theme('DarkAmber') # Add a touch of color
# All the stuff inside your window.
layout = [ [sg.Text('Some text on Row 1')],
[sg.Text('Enter something on Row 2'), sg.InputText()],
[sg.Button('Ok'), sg.Button('Close Window')],
[sg.Text('This is some text', font='Courier 12', text_color='blue', background_color='green')],
[sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]
# Create the Window
window = sg.Window('Test', layout).Finalize()
window.Maximize()
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event in (None, 'Close Window'): # if user closes window or clicks cancel
break
print('You entered ', values[0])
window.close()
I have attempted using PySimpleGui: How to enter text in the text box? but the Text Box here is actually a list box:
which is nothing like the TextBox I want:
The TextBox is surrounded by the red lines. Can someone please help me find the code that will give me the TextBox that I desire?
You can use sg.Multiline(...) which is Text widget of tkinter.
To get the content of the sg.Multiline, you can assign an unique key to it and use this key to get its content in the values dict.
Below is an example based on your code:
import PySimpleGUI as sg
sg.theme('DarkAmber') # Add a touch of color
# All the stuff inside your window.
layout = [ [sg.Text('Some text on Row 1')],
[sg.Text('Enter something on Row 2'), sg.InputText()],
[sg.Button('Ok'), sg.Button('Close Window')],
[sg.Multiline(size=(30, 5), key='textbox')]] # identify the multiline via key option
# Create the Window
window = sg.Window('Test', layout).Finalize()
#window.Maximize()
# Event Loop to process "events" and get the "values" of the inputs
while True:
event, values = window.read()
if event in (None, 'Close Window'): # if user closes window or clicks cancel
break
print('You entered in the textbox:')
print(values['textbox']) # get the content of multiline via its unique key
window.close()