2015-02-07 39 views
0

我正在制作一款游戏,使用文字通知用户游戏中发生了什么。不过,我使用Tkinter窗口来启用按钮输入,以获得更专业的感觉。我还加入了一些标签,但它们都装进了一个新窗口,而不是我已经制作好的一个。不要问变量名,但是这是代码:为什么我的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控制”的小部件,它们出现在一个叫做'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 

当按钮“环视”被点击两次。

有什么方法可以修复这段代码,或者是一种更简单的方法来完成我想要完成的任务吗?问题的

+0

如果您在功能定义之间至少有1个空格来更好地组织代码,那可能会帮助我们找出问题。 – nbro 2015-02-07 22:53:38

+0

为什么你需要其他功能内的功能?我几乎从来没有用过它们,我不知道你为什么需要它们。 – nbro 2015-02-07 22:55:22

+0

@Rinzler,我猜我不知道。我会编辑它。 – 2015-02-08 13:02:56

回答

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的实例。

+0

谢谢 - 你能给出指定父级的语法吗? – 2015-02-08 13:01:29

+0

@OliverPotts:在创建窗口小部件时给出父窗口的第一个参数:'warning = Label(personGUI,...)' – 2015-02-08 13:41:06

+0

现在抛出'Traceback(最近的最后一个窗口): 文件“D:\ Python \ TSG。 py:“行1118,在 personbasecreate() 文件”D:\ Python \ TSG.py“,行917,在personbasecreate warning.pack(personGUI,side ='top') 文件”D:\ Python \ lib \ tkinter \ __ init__.py“,第1927行,在pack_configure中 + self._options(cnf,kw)) _tkinter.TclError:错误选项”-container“:必须是-after,-anchor,-before, - 展开,-fill,-in,-ipadx,-ipady,-padx,-pady或-side – 2015-02-08 14:00:32

相关问题