我正在尝试 pysimplegui 将其用作其他人运行我的代码的 gui。我设法创建了一个 2 窗口 gui,其中第二个窗口供管理员使用。但是我发现如果我要多次进入和退出windows 2,就会出现错误。
下面是会发生错误的代码示例。单击管理>设置时,将弹出第二个窗口。然后,如果我要退出并再输入几次(<4 次),就会发生错误。错误是:
File "<ipython-input-2-4e0963ff7cf8>", line 1, in <module>
runfile('C:/Python/test.py', wdir='C:/Python')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Python/test.py", line 29, in <module>
(ev2, vals2) = win2.Read()
File "C:\ProgramData\Anaconda3\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 5284, in Read
self._Show()
File "C:\ProgramData\Anaconda3\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 5163, in _Show
return _BuildResults(self, False, self)
File "C:\ProgramData\Anaconda3\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 6754, in _BuildResults
_BuildResultsForSubform(form, initialize_only, top_level_form)
File "C:\ProgramData\Anaconda3\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 6843, in _BuildResultsForSubform
value = element.TKIntVar.get()
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 506, in get
value = self._tk.globalgetvar(self._name)
TclError: can't read "PY_VAR60": no such variable
其中“PY_VAR60”中的数字“60”可以更改为任意数字。
import PySimpleGUI as sg
menu_def= [['&Admin',['&Settings','&About']]]
layout1=[
[sg.Menu(menu_def)],
[sg.Text('Input WaferID:', size=(12, 1), font=('Calibri',15))],
[sg.Button('Exit')]
]
layout2 = [
[sg.Checkbox('Rename file?',key='rename', size=(15,1)),sg.Text('Split:')],
[sg.InputText()],
[sg.Button('Exit')]
]
win1 = sg.Window('Oxidation Object Detection V1.0').Layout(layout1)
win2_active = False
while True:
(ev1, vals1) = win1.Read()
if ev1 == 'Settings':
win2_active = True
win2 = sg.Window('Settings').Layout(layout2)
while True:
(ev2, vals2) = win2.Read()
if ev2 == 'Exit' or ev2 == None:
win2.Close()
win2_active = False
break
elif ev1 == 'Exit' or ev1 == None:
win1.Close()
break
我试过用 sg.InputText 替换窗口 2 的复选框,不会有错误。使用复选框时有什么我想念的吗?谢谢你。