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()
Related
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()
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()
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)],
I have added a browse folder button but it not gives me values until I click another button.
I tried reading it like this
while True:
event, values = mainwindow.Read()
if values['Select folder...'] != '':
print(values['Select folder...'])
but the same thing happened.
No layout shown in your issue.
For example, you use sg.FolderBrowse here.
The option target is default as (sg.ThisRow, -1), means element just before your sg.FolderBrowse at same row.
If there's no target in your layout, no event will be generated.
If there's target, event of target will be generated only when target with option enable_events=True.
So, you should have a target element in your layout, most of time, it is sg.InputText with option enable_events=True.
import PySimpleGUI as sg
layout = [
[sg.InputText(enable_events=True), sg.FolderBrowse('Select folder...')],
]
window = sg.Window("Title", layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
print(event, values)
if values['Select folder...'] != '':
print(values['Select folder...'])
window.close()
0 {0: 'D:/Document', 'Select folder...': 'D:/Document'}
D:/Document
There'll be problem if any event generated from `sg.InputText`, like key 'A' pressed. That's not the event you need, and maybe neither element `sg.InputText`. So next example shown only `sg.FolderBrowse`.
import PySimpleGUI as sg
layout = [
[sg.InputText(disabled=True, visible=False, enable_events=True), sg.FolderBrowse('Select folder...')],
]
window = sg.Window("Title", layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
print(event, values)
if values['Select folder...'] != '':
print(values['Select folder...'])
window.close()
0 {0: 'D:/Document', 'Select folder...': 'D:/Document'}
D:/Document
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()