1

从 PySimpleGUI 示例中创建了一个简单的窗口并添加了一些按钮。第一次所有按钮事件都可以正常工作。问题是它似乎第二次锁定在任何按钮上?我得到错误:

回溯(最后一次调用):文件“C:\Python37\GUI\MCC118_Main_01.py”,第 51 行,在 sg.Print("READ press") 文件“C:\Python37\lib\site-packages\PySimpleGUI\ PySimpleGUI.py”,第 5121 行,在 EasyPrint _easy_print_data.Print(*args, end=end, sep=sep) 文件“C:\Python37\lib\site-packages\PySimpleGUI\PySimpleGUI.py”,第 5105 行,在 Print self.Close() File "C:\Python37\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 5108, in Close self.window.Close() AttributeError: 'NoneType' object has no attribute 'Close'


代码是:

#!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
    import PySimpleGUI as sg
else:
    import PySimpleGUI27 as sg

#print = sg.Print
sg.ChangeLookAndFeel('TealMono')

# ------ Menu Definition ------ #
menu_def = [['&File', ['&Open', '&Save', 'E&xit', 'Properties']],
            ['&Edit', ['Paste', ['Special', 'Normal', ], 'Undo'], ],
            ['&Help', '&About...'], ]

# ------ Column Definition ------ #
column1 = [[sg.Text('Column 1', background_color='lightblue', justification='center', size=(10, 1))],
           [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 1')],
           [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 2')],
           [sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 3')]]

layout = [
    [sg.Menu(menu_def, tearoff=True)],
    [sg.Text('Raspberry Pi MCC118 daqhat', size=(30, 1), justification='center', font=("Helvetica", 25), relief=sg.RELIEF_RIDGE)],
    [sg.Text('Continuous scan Number of Channels (0,1,2,3)?' ),
     sg.Checkbox('0', default=True),sg.Checkbox('1', default=True), sg.Checkbox('2', default=True), sg.Checkbox('3', default=True)],
    [sg.Text('Scan rate ( 1 to 1000 Hz ) ?'), sg.Input('10', do_not_clear=True, size=(5, 1), key='_Scan_'), sg.Text('Test' , key='_ScanOut_')],
    [sg.Text('Hat Addresses ?'), sg.Input('0',do_not_clear=True, size=(5, 1), key='_HatAdd_')],
    [sg.Text('Selected HAT device # : '), sg.InputText('0',do_not_clear=True, size=(5, 1), key='_HatSelect_')],
    #[sg.Text('Output : '), sg.Output('')],
    [sg.Multiline(do_not_clear=True, default_text='Samples Read    Scan Count    Channel 0    Channel 1    Channel 2    Channel 3', size=(75, 10), key='_OUTPUT_')],    
    #[sg.Output( size=(80,10)) ],
    #[sg.InputText('Default Folder'), sg.FolderBrowse()],
    [sg.Button('START', button_color=('white', 'green'), key='_btnSTART_'),
     sg.Button('READ', button_color=('white', 'blue'), key='_btnREAD_'),
     sg.Button('STOP', button_color=('white', 'red'), key='_btnSTOP_')],
    [sg.Button('EXIT', button_color=('black', 'red'), key='_btnEXIT_')]]
    #[sg.Submit(tooltip='Click to submit this form'), sg.Cancel()]]

window = sg.Window('Raspberry Pi MCC118 daqhat', default_element_size=(40, 1), grab_anywhere=False).Layout(layout)

while True:                 # Event Loop  
  ev1, val1 = window.Read()  

  if ev1 == '_btnSTART_':  
      # change the "output" element to be the value of "input" element
      # show the output in the shell display      
      window.FindElement('_ScanOut_').Update(val1['_Scan_'])
      sg.Print("START pressed")
  elif ev1 == '_btnREAD_': 
      sg.Print("READ pressed")
  elif ev1 == '_btnSTOP_':    
      sg.Print("STOP pressed")
  # close window X clicked
  elif ev1 is None or ev1 == '_btnEXIT_':  
      break
  else:
      sg.Print("NO event")
  sg.Print(ev1, val1)

window.Close()
4

1 回答 1

1

这是调试打印中的一个错误。

获取一个新版本,它会正常工作。

pip install --upgrade --no-cache-dir PySimpleGUI

于 2018-12-07T13:23:10.367 回答