Reputation: 72
I am incredibly new to working with GUIs (and Python in general) so I decided to attempt the hardest thing I could possibly think of in order to learn the code better. I am designing a game from scratch by myself (it's pretty much a Pokemon rip-off but I have no intention of making any of it publicly available and everything is pulled from open-sources such as bulbapedia). I'm not having too much trouble with the coreEngine code yet; however, I tried to take a break from the Engine and work on the GUI for a bit. I decided to use Tkinter, mostly because I don't know anything about GUIs or any other GUI modules in python, and have run into a problem just a few lines in the script. My code is as follows:
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()
Whenever I call this code, I get the following error:
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
I'm confident this is simply my lack of understanding with Tkinter and GUIs and Menus; however, I can't seem to figure this out... I'm just trying to create a basic drop-down menu to go across the top of the screen (File, Edit, View, Help, etc...) but I have no clue how to do that...
Upvotes: 1
Views: 1511
Reputation: 80
Try this:
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()
The only difference being in line 12(including empty lines) where gameMenu should = Menu().
Upvotes: 1