2015-02-07 59 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

相關問題