我对使用GUI(以及一般的Python)非常陌生,所以我决定尝试一下我能想到的最难的事情,以便更好地学习代码。我自己从头开始设计一款游戏(它几乎是一个口袋妖怪的骗局,但我无意将其中任何一个公开发布,所有内容都来自开放源码,如bulbapedia)。我对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以及菜单缺乏了解;但是,我似乎无法解决这个问题......我只是想创建一个基本的下拉菜单来浏览屏幕的顶部(文件,编辑,查看,帮助等...)但是我不知道怎么做......
答案 0 :(得分:1)
试试这个:
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()
第12行(包括空行)的唯一区别是gameMenu应该= Menu()。