我正在制作一款游戏,使用文字通知用户游戏中发生了什么。不过,我使用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