为什么我的tkinter小部件打包到错误的窗口?

时间:2015-02-07 22:39:59

标签: python tkinter runtime pack traceback

我正在制作一款游戏,使用文字通知用户游戏中发生了什么。但是,我正在使用Tkinter窗口来启用带按钮的输入,以获得更专业的感觉。我还包括一些标签,但它们都包装在一个新窗口而不是我已经制作的窗口。不要询问变量名称,但这是代码:

def scan():
    action=str('scan')
    TSGcontrols.destroy()
    return action
def lookaround():
    action=str('look around')
    TSGcontrols.destroy()
    return action
def visoron():
    action=str('engage visor')
    global firstvisor
    if firstvisor==1:
        firstvisor=int(0)
        print ('The visor has a colour code.')
        print ('It displays a wire frame over black, based on sensor data -\nallowing you to see through walls (to a degree).')
        print ('\nColours:')
        print ('Green = inert object')
        print ('Blue = electrical signature')
        print ('Red = weapon signature')
        print ('Yellow = radiation (the darker the yellow, the deadlier exposure would be)')
        print ('White = life (the more grey, the weaker the lifesigns. Dark grey is dead.)')
        print ('Purple = unidentified\n')
    TSGcontrols.destroy()
    return action
def visoroff():
    action=str('disengage visor')
    TSGcontrols.destroy()
    return action
def personbasecreate():
    global TSGcontrols
    TSGcontrols=tkinter.Tk(screenName='/TSGcontrols',baseName='/TSGcontrols',className='/TSGcontrols')
    warning=Label(text='This is only the control panel for TSG.\nThe game\'s responses are output in the Python window.',bg='red')
    global location
    locationw=Label(text='Location: {0}'.format(location))
    controlling=Label(text='You are controlling only yourself.',bg='blue',fg='white')
    lookaround=Button(text='Look around',command=lookaround)
    visoron=Button(text='Turn visor on',command=visoron)
    visoroff=Button(text='Turn visor off',command=visoroff)
    scan=Button(text='Scan',command=scan)
    warning.pack(parent=TSGcontrols,side='top')
    locationw.pack(parent=TSGcontrols,side='top')
    controlling.pack(parent=TSGcontrols,side='top')
    lookaround.pack(side='left')
    scan.pack(side='left')
    if visor=='on':
        visoroff.pack(parent=TSGcontrols,side='right')
    else:
        visoron.pack(parent=TSGcontrols,side='right')
   groupw.pack(parent=TSGcontrols,side='bottom')

然后是:

addbutton1 = str('no')

while repeat==str('yes'):
    time.sleep(3)
    print ('\nChoose your action.')
    # Creating the basic window:
    personbasecreate()
    if addbutton1=='yes':
        # Adding the additional function:
        leavequarters.pack(in_=personGUI,side='left')
    action=str(personGUI.mainloop())

但是不是出现在名为'TSG Controls'的窗口中的小部件,它们出现在一个名为'tk'的新窗口中 - 所以当窗口被销毁以允许处理变量'action'时,它会破坏一个空的窗口和游戏崩溃,因为函数试图破坏不存在的窗口,抛出错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\Python\lib\tkinter\__init__.py", line 1489, in __call__
    return self.func(*args)
  File "D:\Python\TSG.py", line 881, in lookaround
    personGUI.destroy()
  File "D:\Python\lib\tkinter\__init__.py", line 1849, in destroy
    self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command:  application has been destroyed

单击“环顾四周”按钮两次。

有没有办法解决这个代码,或者更简单的方法来完成我想在这里完成的工作?

1 个答案:

答案 0 :(得分:1)

部分问题可能就是:

personGUI=tkinter.Tk(className='/TSG Controls')
warning=Label(text='This is only the control panel for TSG.\nThe game\'s responses are output in the Python window.',bg='red')

请注意,您正在创建新的根窗口,但之后您创建了一个标签而未指定父级。您应该始终为窗口小部件指定父窗口,并且您应始终只为整个应用程序创建单个Tk实例。如果您需要创建多个窗口,请创建一次根窗口,然后为其他窗口创建tkinter.Toplevel的实例。