我正在尝试将代码从main.py移动到名为GameWindowManager.py的可调用类/模块中
我很陌生,我完全迷失了。
它在main中工作,我可以调用Buttons类,但是当我尝试创建模块GameWindowManager时,我得到一个tlc不是属性错误。
请帮助,如果可能,请简单解释一下。 谢谢。
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 )
答案 0 :(得分:2)
您使用ButtonManager
的方式有点奇怪:当您致电makeButton
时,self
不是ButtonManager
的实例,因为它应符合惯例,但是调用Frame
,这是非常反直觉和过于复杂的。此外,GameWindowManager
不是Frame
,因此您无法在Frame
中使用pack
,master
等__init__
属性和方法
我建议将所有GUI类放在一个模块中。来自其他语言可能看起来很奇怪,但在Python中,在一个模块中有很多类是完全正常的。您可以创建一个超类,其中包含用于添加按钮的所有帮助方法,以及图标内容。然后实际的帧只需要调用超级构造函数并使用定义的帮助方法添加实际的小部件。
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
您的主模块看起来就像这样简单:
import gui
if __name__ == "__main__":
frame = gui.ChronoProjo()
gui.mainloop()