我正在製作一款遊戲,使用文字通知用戶遊戲中發生了什麼。不過,我使用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
當按鈕「環視」被點擊兩次。
有什麼方法可以修復這段代碼,或者是一種更簡單的方法來完成我想要完成的任務嗎?問題的
如果您在功能定義之間至少有1個空格來更好地組織代碼,那可能會幫助我們找出問題。 – nbro 2015-02-07 22:53:38
爲什麼你需要其他功能內的功能?我幾乎從來沒有用過它們,我不知道你爲什麼需要它們。 – nbro 2015-02-07 22:55:22
@Rinzler,我猜我不知道。我會編輯它。 – 2015-02-08 13:02:56