tayyab mahboob
tayyab mahboob

Reputation: 11

Button in PySimpleGUI not working when key is used

I have a basic GUI window with two button. They work properly if there is no key for button. If I use key for any button that button don't work. In this case button 2 is not working, because I have used key='b2' for it.

  import PySimpleGUI as sg

  def main():
    layout=[[sg.Button("button1"),
       sg.Button('button 2',key='b2')]]
    window=sg.Window("Gui",location=(20,20))
    window.Layout(layout).Finalize()
    while True:
      event,values=window.Read()
      if event == 'button1':
       sg.Popup("button 1 pressed")
      if event == 'button 2':
       sg.Popup("button 2 pressed")



  main()

Upvotes: 0

Views: 3236

Answers (2)

Gonzalo
Gonzalo

Reputation: 882

You have to check they key event.

This is a working solution for your example:

import PySimpleGUI as sg                  
                                          
def main():                               
  layout=[[sg.Button("button1"),          
     sg.Button('button2',key='b2')]]      
  window=sg.Window("Gui",location=(20,20))
  window.Layout(layout).Finalize()        
  while True:                             
    event,values=window.Read()            
    if event == 'button1':                
     sg.Popup("button 1 pressed")         
    if event == 'b2':                     
     sg.Popup("button 2 pressed")         
    if event == sg.WIN_CLOSED:            
     break                                                              
                                          
main()                                    
         

                             

Upvotes: 0

Mike from PSG
Mike from PSG

Reputation: 5754

Check the documentation for how to use keys. You're not checking for the key in your if statements. Events are keys.

Upvotes: 1

Related Questions