0

I am trying out pysimplegui to use it as a gui for others to run my code. I manage to create a 2 window gui where the 2nd window is for admin use. But i found that if i were to do a few times of entering and exiting windows 2, there will be an error.

Below is the code example where an error would occur. The 2nd window will popup when click on admin>Settings. Then if i were to exit and enter in a few more times (<4times) an error would occur. The error is:

  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

Where the number "60" in "PY_VAR60" can change to any number.

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

I have tried replacing the checkbox of window 2 with sg.InputText and there would be no error. Is there something i am miss when using checkbox? Thank you.

Zabjaku
  • 97
  • 1
  • 7

3 Answers3

0

I looked over the code, and tried it out myself, but nowhere was the variable 'PY_VAR60' or the number 60 used. The error - TclError: can't read "PY_VAR60": no such variable - suggests that you need to define that somewhere, unless you have done that somewhere else in some other code.

Hope this helped.

Angus
  • 78
  • 8
  • Thank you for trying. I dont know why i still see the error. The above code is what i run and see the error. The thing is i dont have such variable name and everytime i try the number keeps changing. – Zabjaku Aug 27 '19 at 07:41
  • Thats strange, I haven't had much experience with PySimpleGUI, so I hope someone else with more experience can help. Good Luck! – Angus Aug 27 '19 at 08:02
0

After looking through more examples i think i found the answer. It seems the "layout 2" have to be placed after "win2_active = True". I tried a few times of opening and exit and no error. Anyone know why? Corrected code as below:

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')]
        ]


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
      layout2 = [
        [sg.Checkbox('Rename file?',key='rename', size=(15,1)),sg.Text('Split:')],
        [sg.InputText()],
        [sg.Button('Exit')]
        ]

      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
Zabjaku
  • 97
  • 1
  • 7
0

Posting an Issue on the project's GitHub has been suggested as best way to get support on this package. The documentation says that layouts cannot be re-used which is why moving it inside the loop works.

Mike from PSG
  • 5,312
  • 21
  • 39
  • Noted. Thank you. – Zabjaku Aug 28 '19 at 00:50
  • You'll find the explanation here - https://pysimplegui.readthedocs.io/en/latest/#the-golden-rule-of-window-layouts . Also in that same section you'll find design patterns you can follow for multiple window layouts. – Mike from PSG Aug 28 '19 at 00:54