When I use keyboard for dropdown selection, scroll doesn't move up and down,so I am not able to see which one is selected right now. How to move scroll up and down when keyboard is pressed up and down. My sample code is as following:
import PySimpleGUI as sg
class GUI():
def __init__(self):
self.data = [(10), (20), (30), (40), (50), (60), (70), (80), (90), (100)]
self.work_order_currrent_selection_index = 0
def run(self):
layout = [[sg.Listbox(values=self.data, size=(35, 3), enable_events=True, key='selected_key')]]
# Create the Window
self.testWindow = sg.Window('Test', return_keyboard_events=True).Layout(layout).Finalize()
self.testWindow.Maximize()
self.testWindow.Element('selected_key').Update(set_to_index=0)
# Event Loop to process "events"
while True:
event, values = self.testWindow.Read()
if event in('Up:111', '16777235'):
if(hasattr(self, 'testWindow')):
self.work_order_currrent_selection_index = (self.work_order_currrent_selection_index - 1) % len(self.data)
self.testWindow.Element('selected_key').Update(set_to_index=self.work_order_currrent_selection_index)
elif event in ('Down:116',' 16777237'):
if(hasattr(self, 'testWindow')):
self.work_order_currrent_selection_index = (self.work_order_currrent_selection_index + 1) % len(self.data)
self.testWindow.Element('selected_key').Update(set_to_index=self.work_order_currrent_selection_index)
self.testWindow.Close()
if __name__ == '__main__':
app = GUI()
app.run()
When application launch first time I just able to see three dropdown as,
I pressed down arrow key then selection go down one by one like that,
But after selection of 30, on the down key press selection move to next one like 40, 50.. except scrolling, so I am not able to see which one is selected now.
Is there any way to move selection along with scrolling?
See the fourth image, here selection moved to 40 but scroll not moved down. Same issue with up key pressed.
Maybe this will get you a little closer to what you're looking for
import PySimpleGUI as sg
class GUI():
def __init__(self):
self.data = [(10), (20), (30), (40), (50), (60), (70), (80), (90), (100)]
self.work_order_currrent_selection_index = 0
def run(self):
layout = [[sg.Listbox(values=self.data, size=(35, 3), enable_events=True, key='selected_key')]]
# Create the Window
self.testWindow = sg.Window('Test', layout, return_keyboard_events=True, finalize=True)
# self.testWindow.Maximize()
self.testWindow.Element('selected_key').Update(set_to_index=0)
# Event Loop to process "events"
while True:
event, values = self.testWindow.Read()
if event is None:
break
if event.startswith('Up'):
self.work_order_currrent_selection_index = (self.work_order_currrent_selection_index - 1) % len(self.data)
self.testWindow['selected_key'].Update(set_to_index=self.work_order_currrent_selection_index,scroll_to_index=self.work_order_currrent_selection_index )
elif event.startswith('Down'):
self.work_order_currrent_selection_index = (self.work_order_currrent_selection_index + 1) % len(self.data)
self.testWindow['selected_key'].Update(set_to_index=self.work_order_currrent_selection_index, scroll_to_index=self.work_order_currrent_selection_index)
self.testWindow.Close()
if __name__ == '__main__':
app = GUI()
app.run()
Related
I'm trying to create the below piece of code for video and subtitle sync.
I need the first window to get the videos and the second window to get the subtitles upon drag and drop.
However as per the code now whenever i drag and drop onto one of the "windows/boxes" it shows in the other one as if they are functioning as one box even though i have separate key for them.
appreciate your guidance on how to separate them.
thank you
import PySimpleGUIQt as sg
class Listbox(sg.Listbox):
def dragEnterEvent(self, e):
e.accept()
def dragMoveEvent(self, e):
e.accept()
def dropEvent(self, e):
data1 = window['-VIDEOLIST-'].get_list_values()
data2 = window['-SUBTITLESLIST-'].get_list_values()
items = [str(v) for v in e.mimeData().text().strip().split('\n')]
print(items)
data1.extend(items)
data2.extend(items)
window['-VIDEOLIST-'].update(data1)
window['-SUBTITLESLIST-'].update(data2)
window.refresh()
def enable_drop(self):
# Called after window finalized
self.Widget.setAcceptDrops(True)
self.Widget.dragEnterEvent = self.dragEnterEvent
self.Widget.dragMoveEvent = self.dragMoveEvent
self.Widget.dropEvent = self.dropEvent
layout = [
[sg.Text("Choose a Video: "), sg.Input(), sg.FileBrowse(key="-VID-"),sg.Text("Choose a Subtitle: "), sg.Input(), sg.FileBrowse(key="-SUB-")],
[Listbox([], size=(50, 10), enable_events=True, key='-VIDEOLIST-'),Listbox([], size=(50, 10), enable_events=True, key='-SUBTITLESLIST-')],
[sg.Button('Auto Sync', key='-AutoSync-')]
]
window = sg.Window("Title", layout, finalize=True)
window['-VIDEOLIST-'].enable_drop()
window['-SUBTITLESLIST-'].enable_drop()
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
window.close()
I have a quick question about updating fields dynamically in a multiple window setup such as the example above. I want it so that when the user selects autofill, values[SAVEINVENTORYPATH] is printed to the 'testinputtext' field in the settings window however when I use window.update, it searches for the keys in the main window rather than the settings window. How can I direct PySimpleGUI towards the settings window?
EDIT: Thank you for your help, I have edited the code and the autofill function works however currently, Autofill appears to freeze the window but I want people to be able to select Autofill and then hit Save and then the Settings window refreshes however I'm not sure where to currently place the while True loop. Thank you again for your assistance with this.
def create_settings_window(settings):
sg.theme('LightGrey6')
settings = load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
def TextLabel(text): return sg.Text(text+':', justification='r', size=(20,1))
layout = [ [sg.Text('Set Up Connection', font='Any 15')],
[TextLabel('Inventory List'), sg.Input(key='SAVEINVENTORYPATH'), sg.FileBrowse(key='INVENTORYLIST')],
[sg.Text(text='', key = 'testinputtext')],
[sg.Button('Save'), sg.Button('Autofill'), sg.Button('Exit')] ]
window = sg.Window('Settings', layout, keep_on_top=True, finalize=True,element_padding = (3,3.5))
test = window['testinputtext']
event, values = window.read()
if event == 'Autofill':
test.update(value = 'hi')
if event == 'Save':
save_settings(SETTINGS_FILE, settings, values)
sg.popup('Settings saved')
else:
print(event, values)
for key in SETTINGS_KEYS_TO_ELEMENT_KEYS:
try:
window[SETTINGS_KEYS_TO_ELEMENT_KEYS[key]].update(value=settings[key])
except Exception as e:
print(f'Problem updating PySimpleGUI window from settings. Key = {key}')
return window
def main():
window, settings = None, load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
while True: # Event Loop
#LAYOUT
if window is None:
settings = load_settings(SETTINGS_FILE, DEFAULT_SETTINGS )
sg.theme('LightGrey6')
layout = [[sg.Multiline(size=(180,10),key='Output',pad=(0,20))],
[sg.Button(button_text = 'Settings',key='Settings'),sg.Button(button_text = 'Load Output',key='LOAD_OUTPUT'),sg.Exit()]]
window = sg.Window('Settings Dynamic Update Test', layout, element_justification='center', size= (600,300))
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
window.close()
if event == 'Settings':
create_settings_window(settings)
else:
print(event, values)
window.close()
main()
It' better to handle each window in it's own event loop, mix windows together may get things mush difficult or complex.
Following example show the way to go.
import PySimpleGUI as sg
def popup():
sg.theme('DarkBlue4')
layout = [
[sg.Text("You name"), sg.Input("", key='Name')],
[sg.Text("", size=0, key='Test')],
[sg.Button('Save'), sg.Button('Auto Fill'), sg.Button('Cancel')],
]
window = sg.Window('Settings', layout, modal=True)
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Cancel'):
flag = False
break
elif event == 'Save':
settings['Name'] = values['Name']
flag = True
break
elif event == 'Auto Fill':
window['Test'].update(values['Name'])
window.close()
return flag
def main_window():
sg.theme('DarkBlue3')
name = settings['Name']
value = name if name else 'None'
layout = [
[sg.Text("User Name:"), sg.Text(value, key='Name')],
[sg.Button('Settings'), sg.Button('Exit')],
]
return sg.Window('Main', layout)
settings = {'Name':None}
window = main_window()
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
elif event == 'Settings':
flag = popup()
if flag:
window.close()
window = main_window()
""" or just update element
name = settings['Name']
value = name if name else 'None'
window['Name'].update(value)
"""
window.close()
I'm using PySimpleGUI (version 4.55.1) to make a GUI for a python-based Window application.
There are many screens in this application.
For security reason, I need to implement:
"If there is no actions on GUI for a certain time (e.g, 1min), then the program will automatically back to the top screen"
Is there any way I can achieve that with PysimpleGUI?
The idea only would also help me a lot.
Thanks
There's option auto_close=True in sg.Window, but it doesn't work for multi-window sg.read_all_windows now.
So more complex job to do by counting the duration by yourself. Two ways for it
Count by timeout event in main_window, it make more programming techniques required.
Count by element.Widget.after in sub_window, it make code more clear, but with tkinter code.
Demo code for case 1.
import PySimpleGUI as sg
class Marqueen(sg.Text):
def __init__(self, texts, size=80, font=('Courier New', 11), sep=' ~~~ ',
t=200, fg='white', bg='green'):
self.texts = texts
self.size = size
self.font = font
self.sep = sep
self.t = t
self.running = False
self.index = 0
super().__init__('', size=size, font=self.font, relief=sg.RELIEF_SUNKEN,
text_color=fg, background_color=bg)
def start(self):
message = self.sep.join(self.texts)
text = message
while len(text) < self.size:
text += self.sep + message
self.text = text + self.sep + text[:self.size]
self.limit = len(text+self.sep)
self.running = True
self.run()
def run(self):
if not self.running:
return
self.update(self.text[self.index:self.index+self.size])
self.update()
self.index += 1
if self.index == self.limit:
self.index = 0
self.Widget.after(self.t, self.run)
def pause(self):
self.running = False
def stop(self):
self.running = False
self.index = 0
def new_window(index, duration=5):
marqueen = Marqueen(news[index])
layout = [[marqueen]]
window = sg.Window(f'News', layout, finalize=True, no_titlebar=True,
location = (450, 50*(index+2)))
window.marqueen = marqueen
window.duration = duration
return window
font = ("Courier New", 16)
sg.theme("DarkGreen1")
sg.set_options(font=font)
index = 0
limit = 5
sub_wins = []
news = [
["France to start vaccinating children aged 5-11"],
["Comet Leonard has been dazzling the night sky in a pre-Christmas show"],
["Plans unveiled for high-tech '10-minute city' in Seoul"],
["France's $19 billion weapons deal is sweet revenge"],
["'Koala massacre' prompts hundreds of cruelty charges"],
]
frame = [[sg.Checkbox(f'Sub Window {i+1}', disabled=True, key=f'Check {i}')] for i in range(limit)]
layout = [[sg.Frame('', frame), sg.Button('New')]]
window = sg.Window("Multi-Window", layout, location=(100, 100), size=(300, 220), finalize=True)
for i in range(limit):
window[f'Check {i}'].Widget.configure(disabledforeground='white')
while True:
win, event, values = sg.read_all_windows(timeout=100)
if event in (sg.WINDOW_CLOSED, 'Exit'):
if win in sub_wins:
win.marqueen.stop()
win.close()
sub_wins.remove(win)
index -= 1
window[f'Check {index}'].update(value=False)
for i, w in enumerate(sub_wins):
w.move(450, 50*(i+2))
else:
break
elif event == 'New':
if index == limit:
continue
w = new_window(index)
w.marqueen.start()
sub_wins.append(w)
window[f'Check {index}'].update(value=True)
index += 1
elif event == sg.TIMEOUT_EVENT and sub_wins:
for w in sub_wins:
w.duration -= 0.1
if w.duration <= 0:
w.write_event_value(None, None)
for win in sub_wins:
win.close()
window.close()
Here's another most-simple demo code for case 2
from random import randint
import PySimpleGUI as sg
def sub_win(duration=5):
text = 'Sub Window'
location = (randint(100, width-100), randint(100, height-100))
layout = [[sg.Text(text)]]
window = sg.Window(text, layout, location=location, finalize=True)
window.TKroot.after(int(duration*1000), lambda win=window:win.write_event_value(sg.WINDOW_CLOSED, None))
return window
width, height = sg.Window.get_screen_size()
layout = [[sg.Button('New Window')]]
window = sg.Window('Main Window', layout, keep_on_top=True, finalize=True)
windows = []
while True:
win, event, values = sg.read_all_windows()
if event in (sg.WINDOW_CLOSED, 'Exit'):
if win in windows:
windows.remove(win)
win.close()
else:
break
elif event == 'New Window':
w = sub_win()
windows.append(w)
for w in windows:
w.close()
window.close()
I am Trying to make a tkinter based Cube Timer Program for which I have done a console based program before I wanted to improve on it by making a GUI for it... I have made some tkinter projects before and I have a decent knowledge of how stuff works the thing is I want the Label in the tkinter window to update in every second while listening for a spacebar key on press I couldn't figure out a way to do it can someone help me?
def on_press(key):
global chk,start,end,timer_label
print('{0} pressed'.format(key))
if key == Key.space:
if chk == True:
end = time.time()
chk = False
messagebox.showinfo('Your Time',"Your Time =>"+str(round(end-start,3)))
def on_release(key):
global chk,start,end,timer_label
if key == Key.space:
if chk == False:
start = time.time()
chk = True
with Listener(on_press=on_press,on_release=on_release) as listener:
main = tk.Tk()
main.title("Cube Timer Mark 4 Prototype")
main.resizable(0,0)
tk.Label(main, text="Cube Time Mk3 Prototype", font=['Consolas',16]).grid(row = 0,column=0)
textbox = tk.Text(main,font=['Consolas',20], height = 1,width = 27)
textbox.grid(row = 1,column=0)
textbox.insert('1.0',scrambler())
timer_frame = tk.Frame(main).grid(row=2,column=0)
global timer_label
timer_label = tk.StringVar()
timer_label.set("0.0")
tk.Label(timer_frame,text = timer_label.get()+"s",font=['Consolas',20,'bold'],pady = 25).grid(row = 2,column = 0)
main.mainloop()
This is what I have tried but didn't work
You need main.after to periodically run a function like updating the label. To listen to keyboard event, you need main.bind. See example below.
Enhancement: use tk.Label(..., textvariable=timer_label) means the label will automatically update when you set timer_label
import tkinter as tk
def key_press(event):
if event.keysym == 'space':
print('pressed')
def key_release(event):
if event.keysym == 'space':
print('released')
def update_label():
# get the time from the string
time = float(timer_label.get()[:-1])
# increment the time and put it back in timer_label
timer_label.set(str(time+1) + 's')
# calling this function again 1000ms later, which will call this again 1000ms later, ...
main.after(1000, update_label)
main = tk.Tk()
main.title("Cube Timer Mark 4 Prototype")
main.resizable(0,0)
tk.Label(main, text="Cube Time Mk3 Prototype", font=['Consolas',16]).grid(row = 0,column=0)
textbox = tk.Text(main,font=['Consolas',20], height = 1,width = 27)
textbox.grid(row = 1,column=0)
#textbox.insert('1.0',scrambler())
timer_frame = tk.Frame(main).grid(row=2,column=0)
timer_label = tk.StringVar()
timer_label.set("0.0s")
# using textvariable, you can simply update timer_label and it will be reflected
tk.Label(timer_frame,textvariable = timer_label ,font=['Consolas',20,'bold'],pady = 25).grid(row = 2,column = 0)
# bind keypress and keyrelease to the functions
main.bind("<KeyPress>", key_press)
main.bind("<KeyRelease>", key_release)
# call update label function for the first time
update_label()
main.mainloop()
I am trying to get a better understanding of how wxPython 'scans'.
Please see my code below:
import os
import wx
from time import sleep
NoFilesToCombine = 0
class PDFFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title, size=(400,400))
panel = wx.Panel(self)
self.Show()
try: #Set values of PDFNoConfirmed to zero on 1st initialisation
if PDFNoConfimed != 0:
None
except UnboundLocalError:
PDFNoConfimed = 0
try: #Set values of PDFNoConfirmed to zero on 1st initialisation
if PDFNoConfirmedInitially != 0:
None
except UnboundLocalError:
PDFNoConfirmedInitially = 0
while ((PDFNoConfimed == 0) and (PDFNoConfirmedInitially == 0)):
while PDFNoConfirmedInitially == 0:
BoxInputNo = wx.NumberEntryDialog(panel, "So You Want To Combine PDF Files?", "How Many?", "Please Enter", 0, 2, 20)
if BoxInputNo.ShowModal() == wx.ID_OK: #NumberEntryDialog Pressed OK
NoFilesToCombine = BoxInputNo.GetValue()
PDFNoConfirmedInitially = 1
elif BoxInputNo.ShowModal() == wx.ID_CANCEL:
exit()
print(NoFilesToCombine)
ConfirmationLabel = wx.StaticText(panel, -1, label="You Have Selected " + str(NoFilesToCombine) + " Files To Combine, Is This Right?", pos=(20, 100))
ConfirmationBoxConfirm = wx.ToggleButton(panel, label="Confirm", pos=(20, 200))
ConfirmationBoxCancel = wx.ToggleButton(panel, label="Cancel", pos=(180, 200))
#if ConfirmationBoxConfirm.GetValue() == 1:
# exit()
if ConfirmationBoxCancel.GetValue() == 1:
PDFNoConfirmedInitially = 0
app = wx.App()
frame = PDFFrame(None, title="Robs PDF Combiner Application")
app.MainLoop()
Now this is a work in progress so it obviously isn't complete. However what I'm trying to accomplish with the above is:
Display a number entry popup. If user presses 'cancel' exit the application (this works but needs 2 presses for some reason?). If press OK, then:
Display the number entered in step 1, with 2 additional buttons. The 'confirm' doesn't do anything as yet, but the 'cancel' should take you back to step 1. (by resetting the PDFNoConfirmedInitially flag).
Now step 2 doesn't work. When I debug it almost appears as though the PDFFrameonly gets scanned once. My presumably false assumption being that this would be continually scanned due to app.MainLoop() continually scanning wx.App() which in turn would call the child frame?
Help/ pointers/ deeper understanding always appreciated,
Thanks
Rob
1) ShowModal() shows dialog window and you use it two times
if BoxInputNo.ShowModal() == wx.ID_OK:
and
elif BoxInputNo.ShowModal() == wx.ID_CANCEL:
so it shows your window two times.
And only at second time you check wx.ID_CANCEL.
You should run it only once and check its result
result = BoxInputNo.ShowModal()
if result == wx.ID_OK:
pass
elif result == wx.ID_CANCEL:
self.Close()
return
2) You have to assign function to button and this function should reset variable and show dialog window again. But I think wx.Button could be better then wx.ToggleButton
ConfirmationBoxCancel = wx.Button(panel, label="Cancel", pos=(180, 200))
ConfirmationBoxCancel.Bind(wx.EVT_BUTTON, self.on_button_cancel)
def on_button_cancel(self, event):
#print('event:', event)
pass
# reset variable and show dialog window
Frankly I don't understand some of your variables. Maybe if you use True/False instead of 0/1 then they will be more readable. But main problem for me are while loops. GUI frameworks (wxPython, tkinter, PyQt, etc) should run only one loop - Mainloop(). Any other loop may block Mainloop() and GUI will freeze.
I created own version without any while loop but I don't know if it resolve all problems
import wx
class PDFFrame(wx.Frame):
def __init__(self, parent, title):
super().__init__(parent, -1, title, size=(400,400))
self.panel = wx.Panel(self)
self.Show()
# show dialog at start
if self.show_dialog(self.panel):
# create StaticLabel and buttons
self.show_confirmation(self.panel)
else:
# close main window and program
self.Close()
def show_dialog(self, panel):
"""show dialog window"""
global no_files_to_combine
box_input_no = wx.NumberEntryDialog(panel, "So You Want To Combine PDF Files?", "How Many?", "Please Enter", 0, 2, 20)
result = box_input_no.ShowModal()
if result == wx.ID_OK: #NumberEntryDialog Pressed OK
no_files_to_combine = box_input_no.GetValue()
return True
elif result == wx.ID_CANCEL:
print('exit')
return False
def show_confirmation(self, panel):
"""create StaticLabel and buttons"""
self.confirmation_label = wx.StaticText(self.panel, -1, label="You Have Selected {} Files To Combine, Is This Right?".format(no_files_to_combine), pos=(20, 100))
self.confirmation_box_confirm = wx.Button(self.panel, label="Confirm", pos=(20, 200))
self.confirmation_box_cancel = wx.Button(self.panel, label="Cancel", pos=(180, 200))
# assign function
self.confirmation_box_confirm.Bind(wx.EVT_BUTTON, self.on_button_confirm)
self.confirmation_box_cancel.Bind(wx.EVT_BUTTON, self.on_button_cancel)
def update_confirmation(self):
"""update existing StaticLabel"""
self.confirmation_label.SetLabel("You Have Selected {} Files To Combine, Is This Right?".format(no_files_to_combine))
def on_button_cancel(self, event):
"""run when pressed `Cancel` button"""
#print('event:', event)
# without `GetValue()`
if self.show_dialog(self.panel):
# update existing StaticLabel
self.update_confirmation()
else:
# close main window and program
self.Close()
def on_button_confirm(self, event):
"""run when pressed `Confirn` button"""
#print('event:', event)
# close main window and program
self.Close()
# --- main ---
no_files_to_combine = 0
app = wx.App()
frame = PDFFrame(None, title="Robs PDF Combiner Application")
app.MainLoop()