【发布时间】:2014-11-07 21:11:27
【问题描述】:
我对使用 GUI(以及一般的 Python)非常陌生,因此我决定尝试我能想到的最困难的事情,以便更好地学习代码。我正在自己从零开始设计一款游戏(这几乎是一个 Pokemon 的翻版,但我无意将其中的任何内容公开,并且所有内容都来自开源软件,例如 bulapedia)。我对 coreEngine 代码还没有太多麻烦;然而,我试着从引擎中休息一下,在 GUI 上工作了一会儿。我决定使用 Tkinter,主要是因为我对 Python 中的 GUI 或任何其他 GUI 模块一无所知,并且在脚本中仅几行就遇到了问题。我的代码如下:
from Tkinter import *
import battleEngine
import openingSequence
from classes import Pokemon
from classes import movesList
def donothing():
print "Doesn't do anything."
class gameWindow(Frame):
def displayMenu(self):
gameMenu = Menu
fileMenu = Menu(gameMenu, tearoff=0)
fileMenu.add_command(label="New Game", command=donothing)
fileMenu.add_command(label="Load Game", command=donothing)
fileMenu.add_command(label="Save Game", command=donothing)
fileMenu.add_command(label="Save Game as...", command=donothing)
fileMenu.add_command(label="Exit", command=exit())
def __init__(self):
self.displayMenu()
inGame = gameWindow()
inGame.mainloop()
每当我调用此代码时,都会收到以下错误:
Traceback (most recent call last):
File "C:/path/to/file/pokemonGui.py", line 26, in <module>
inGame = gameWindow()
File "C:/path/to/file/pokemonGui.py", line 24, in __init__
self.displayMenu()
File "C:/path/to/file/pokemonGui.py", line 15, in displayMenu
fileMenu = Menu(gameMenu, tearoff=0)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2642, in __init__
Widget.__init__(self, master, 'menu', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2027, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2005, in _setup
self.tk = master.tk
AttributeError: class Menu has no attribute 'tk'
Process finished with exit code 1
我相信这只是我对 Tkinter 和 GUI 和菜单缺乏了解;但是,我似乎无法弄清楚...我只是想创建一个基本的下拉菜单来浏览屏幕顶部(文件、编辑、查看、帮助等...)但是我不知道该怎么做...
【问题讨论】:
-
gameMenu = Menu应该是gameMenu = Menu()。否则 gameMenu 是类本身,而不是其实例
标签: python user-interface tkinter