Widget appears after the user's input - python

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()

Related

Want to be able to drag the things in the listbox into the workspace

I am trying to get the content in the listbox able to be dragged into the workspace as a block. Like scratch. I used OpenAi to give me ideas of how to do it, and it gave me that there is a draggable option I can put into the code as you can see, but when I try to drag the items in the listbox it wont let me.
import PySimpleGUI as sg
from PIL import ImageGrab
# Create a list of Python code to display in the scroller
code_lines = [
"variable",
"loops",
"types of variables"
]
# Create the window layout
layout = [ [sg.Column([[sg.Listbox(values=code_lines, size=(20, 10), key='Python Code', draggable = True)]]),
sg.Column([[sg.Text('Workspace:', size=(20, 1)), sg.Multiline(size=(50, 30), key='workspace', background_color='dark gray')]])],
[sg.Button('Close')] ]
# Create the window
window = sg.Window('PyDrop', layout)
# Event loop to process events
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Close': # if user closes window or clicks close button
break
elif event == 'Python Code': # if user clicks on an item in the listbox
# stores the value of the clicked item in a variable
dragged_item = values['Python Code'][0]
elif event == sg.DRAG: # if user moves the mouse while holding down the button
# updates the value of the drop target
print('Drag event triggered')
window['drop_target'].update(dragged_item)
# Close the window
window.close()

Is there a way to visually hold a button after it's been pressed in PySimleGUI?

I want to replace a dropdown with the user clicking an option instead.
I currently have this:
I need this instead:
Here's my code (contains pseudocode because I don't know how "deselecting" would work either:
import PySimpleGUI as sg
layout = [
[ sg.Text("Testing") ],
[sg.Input(key="in")],
[ sg.Text ("Language?"), sg.Button("German", key="ger"), sg.Button("French", key="fr")],
]
window = sg.Window("This is for testing purposes", layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
if event == "ger":
deselect("fr") if "fr" is selected
option = "german"
...
if event == "fr":
deslect("ger") if "ger" is selected
option = "french"
...
In short, I want to switch from dropdown selection to buttons that show the user that they're currently selecting button X as their option, and if they select another button (button Y), then it would unselect button X and show that button Y is now selected instead.
There's no option indicatoron provided now, but tkinter code will work for it.
Normally a radiobutton displays its indicator. If you set this option to zero, the indicator disappears, and the entire widget becomes a “push-push” indicatoron button that looks raised when it is cleared and sunken when it is set. You may want to increase the borderwidth value to make it easier to see the state of such a control.
import PySimpleGUI as sg
layout = [
[sg.Text("Testing") ],
[sg.Input(key="in")],
[sg.Text ("Language?"),
sg.Radio("German", "Language", key="ger"),
sg.Radio("French", "Language", key="fr")],
]
window = sg.Window("This is for testing purposes", layout, finalize=True)
for key in ("ger", "fr"):
window[key].widget.configure(indicatoron=False)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
window.close()

Python PySimpleGUi suexpected behavior trying to "copy-paste" inputText

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.

How do I create a custom button using PySimpleGUI that will allow me to browse for files?

I'm currently trying to create a custom button in python using PySimpleGUI that allows me to browse for files. I am able to do this successfully for a button that submits my entries and one that cancels the process, but am unable to figure out how to do so without having to use sg.FilesBrowse(), which doesn't allow me to customize the button.
I have the current code for my window.
#select theme for GUI
sg.theme('Light Blue 2')
#create custom buttons
submit_button = sg.Button('', image_data=submit_base64,button_color=(sg.theme_background_color(),sg.theme_background_color()),border_width=0, key='Sumbit')
cancel_button = sg.Button('', image_data=cancel_base64,button_color=(sg.theme_background_color(),sg.theme_background_color()),border_width=0, key='Cancel')
#create GUI layout
layout = [[sg.Text('Please select the following files:', font = ('bold', 14))],
[sg.Text('Page Names', size=(15, 1)), sg.Input(),sg.FileBrowse()],
[sg.Text('Files to Replicate', size=(15, 1)), sg.Input(), sg.FilesBrowse()],
[submit_button, cancel_button]]
In place of sg.FileBrowse() and sg.FilesBrowse() I would like to have my custom buttons similarly to how I created submit_button and cancel_button. I tried to follow the same format but am unsure what key I would have to use to have the button behave just like sg.FileBrowse() and sg.FilesBrowse().
Thanks.
Try this.
Create a button
When button pressed call popup_get_file
Take results from popup and fill in the Input element
import PySimpleGUI as sg
def main():
layout = [ [sg.Text('My Window')],
[sg.Input(key='-IN-'), sg.Button('MyBrowse', key='-BROWSE-')],
[sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == '-BROWSE-':
file = sg.popup_get_file('', no_window=True)
window['-IN-'].update(file)
window.close()
if __name__ == '__main__':
main()

PySimpleGUI make text selectable with mouse

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()

Categories

Resources