Make a Textbox in PySimpleGUI - python

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

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

After entering a text in the input of PysimpleGUI, how do I put the cursor to the end of the input text automatically?

I have created a pysimplegui window, like the following:
Now, as I input a text, the input text is shown as the following:
But I want the cursor to be located to the right of the text automatically as the text is entered, like the following:
This is my code for the window:
# Creating the GUI window.
sg.theme('DarkAmber') # Add a touch of color
# All the stuff inside your window.
lengthOfInputBar = 40 # Length of the bar where inputs are entered.
layout = [ [sg.Text(text='Valgrind Message .txt File Location'),
sg.Input(default_text='', size=(lengthOfInputBar, 1),
enable_events=True, key='-VALGRIND_OUTPUT_TXT_INP-'),
sg.FileBrowse(file_types=[('TXT (*.txt)', '*.txt')])] ]
How do I do that?
There's no existing method to view/move cursor to end of sg.Input.
Call tkinter method xview_moveto(f) to positions the text in the sg.Input, the f argument must be in the range [0,1], where 0 means the left end of the text and 1 the right end.
Call tkinter method icursor(index) to set the insertion cursor just before the character at the given index, end` for end of line.
Example Code
import PySimpleGUI as sg
sg.theme('DarkAmber')
lengthOfInputBar = 40
layout = [
[sg.Text(text='Valgrind Message .txt File Location'),
sg.Input(default_text='', size=(lengthOfInputBar, 1), enable_events=True, key='-VALGRIND_OUTPUT_TXT_INP-'),
sg.FileBrowse(file_types=[('TXT (*.txt)', '*.txt')])],
]
window = sg.Window('Songs list', layout, finalize=True)
entry = window['-VALGRIND_OUTPUT_TXT_INP-'].Widget
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == '-VALGRIND_OUTPUT_TXT_INP-':
entry.xview_moveto(1) # View end
entry.icursor('end') # Move cursor to end
window.close()

PySimpleGui - how do you remove text from input text box

How do you clear text in an input box PySimpleGui? I'm trying window ['-INPUT-'] ('') but I recieve a key error. I want it so that the text in the box gets replaced with an empty string after each iteration so the user doesn't have to delete the text themselves.
I want it so that the text in the box gets replaced with an empty
string after each iteration so the user doesn't have to delete the
text themselves.
Ah!
There is a parameter meant specifically for this purpose.
Set do_not_clear=False
import PySimpleGUI as sg
layout = [ [sg.Text('Input element that clears after every read')],
[sg.Input('Initial text', key='-I-', do_not_clear=False)],
[sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('Input auto-clear', layout)
while True: # Event Loop
event, values = window.read()
if event in (None, 'Exit'):
break
print(event, values)
window.close()

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