Reputation: 53
I am trying to move code from a main.py into a callable class/module called GameWindowManager.py
I'm so new at this I'm completely lost.
It works in main and I can call the Buttons class but when I try to Make the module GameWindowManager I get a tlc is not an attribute error.
Please help and if possible explain it simplisticly. Thank you.
main.py
'''
main.py
Created on Oct 2, 2013
@author: noel
ChronoProjo Vesion 3.0
'''
from tkinter import *
from chrono.button import *
from chrono.GameWindowManager import *
class ChronoProjo(Frame):
"""self is the main programe"""
def __init__(self,master=None):
"""Initialize your self"""
"""Display the curent Operating System"""
print (sys.platform)
"""Initialise the base class"""
Frame.__init__(self)
ButtonManager.makeButton ( self, "new" )
ButtonManager.makeButton ( self, "load" )
ButtonManager.makeButton ( self, "quit" )
ButtonManager.makeButton ( self, "save as" )
"""Set the Window Title and Icon"""
self.master.title ( "Chrono Generic Toolbar" )
if sys.platform == 'win32':
#make an icon here
pass
self.pack ( side=TOP, fill=X )
root = Tk()
#create a toolbar
if __name__ == "__main__":
ChronoProjo().mainloop()
button.py
'''
button.py
Created on Oct 4, 2013
@author: noel
button Vesion 1.0
'''
from tkinter import *
#from tkinter.messagebox import *
class ButtonManager():
def makeButton(self, name):
if name == "new":
menuButton = Button ( self, text = name, width = 11, command = ButtonManager.newGameCallback )
elif name == "load":
menuButton = Button(self, text = name, width = 11, command = ButtonManager.loadCallback)
elif name == "quit":
menuButton = Button(self, text = name, width = 11, command = ButtonManager.quitCallback)
elif name == "save as":
menuButton = Button(self, text = name, width = 11, command = ButtonManager.saveAsCallback)
menuButton.pack(side=LEFT, padx=2, pady=2)
def newGameCallback(self):
print ("called the new callback!")
def loadCallback(self):
print ("called the load callback!")
def quitCallback(self):
print ("called the quit callback!")
def saveAsCallback(self):
print ("called the save as callback!")
GameWindowManager.py
'''
GameWindowManager.py
Created on Oct 14, 2013
@author: noel
'''
from tkinter import *
from chrono.button import *
class GameWindowManager(object):
'''
This is to manage the various game windows that will be used throughout the game
'''
def __init__(self,frame):
"""Initialize your self"""
"""Display the curent Operating System"""
print (sys.platform)
"""Initialise the base class"""
Frame.__init__(self)
ButtonManager.makeButton ( self, "new" )
ButtonManager.makeButton ( self, "load" )
ButtonManager.makeButton ( self, "quit" )
ButtonManager.makeButton ( self, "save as" )
"""Set the Window Title and Icon"""
self.master.title ( "Chrono Generic Toolbar" )
if sys.platform == 'win32':
self.master.wm_iconbitmap ( './assets/img/NLFFace.ico' ) #haven't worked out how to do self in Linux yet
pass
self.pack ( side=TOP, fill=X )
Upvotes: 1
Views: 77
Reputation: 82929
The way you use ButtonManager
is somewhat weird: When you call makeButton
, self
is not an instance of ButtonManager
, as it should be by convention, but the calling Frame
, which is very counter-intuitive and overly complicated. Also, GameWindowManager
is not a Frame
, so you can not use Frame
attributes and methods like pack
, master
, etc. in __init__
.
I would recommend to put all your GUI classes in one module. Coming from other languages this may seem odd, but in Python it's perfectly normal to have many classes in one module. You could create one superclass holding all your helper methods for adding buttons, as well as the icon-stuff. The actual frames then only have to call the super-constructor and add the actual widgets using the defined helper methods.
from tkinter import *
class AbstractFrame(Frame):
def __init__(self, title):
Frame.__init__(self)
self.master.title(title)
if sys.platform == 'win32':
self.master.wm_iconbitmap('./assets/img/NLFFace.ico')
self.pack(side=TOP, fill=X)
def makeButton(self, name, command):
menuButton = Button (self, text=name, width=11, command=command)
menuButton.pack(side=LEFT, padx=2, pady=2)
# other helper methods like this
class ChronoProjo(AbstractFrame):
def __init__(self):
AbstractFrame.__init__(self, "Chronopia Generic Toolbar")
self.makeButton("new", self.newGameCallback)
self.makeButton("load", self.loadCallback)
self.makeButton("quit", self.quitCallback)
self.makeButton("save as", self.saveAsCallback)
def newGameCallback(self):
print ("called the new callback!")
# other callback methods
# other frames
You main module then looks as simple as this:
import gui
if __name__ == "__main__":
frame = gui.ChronoProjo()
gui.mainloop()
Upvotes: 2