【问题标题】:Button in PySimpleGUI not working when key is used使用键时 PySimpleGUI 中的按钮不起作用
【发布时间】:2019-08-27 11:59:12
【问题描述】:

我有一个带有两个按钮的基本 GUI 窗口。如果没有按钮键,它们可以正常工作。如果我对任何按钮使用键,则该按钮不起作用。在这种情况下,按钮 2 不起作用,因为我使用了 key='b2'

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

【问题讨论】:

  • 您不需要完成窗口。此外,您可以将其设为单行,因为布局也是一个参数:window=sg.Window("Gui",layout, location=(20,20)) 是您创建窗口所需的全部内容。

标签: key pysimplegui


【解决方案1】:

查看文档以了解如何使用密钥。您没有检查 if 语句中的键。事件是关键。

【讨论】:

    【解决方案2】:

    你必须检查他们的关键事件。

    这是您示例的有效解决方案:

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

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 1970-01-01
      • 2015-06-22
      • 2023-01-31
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      相关资源
      最近更新 更多