1

我正在尝试将代码从 main.py 移动到一个名为 GameWindowManager.py 的可调用类/模块中,
我对此很陌生,我完全迷路了。
它在 main 中工作,我可以调用 Buttons 类,但是当我尝试制作模块 GameWindowManager 时,我得到一个 tlc 不是属性错误。

请帮助,如果可能的话,简单地解释一下。谢谢你。

主文件

'''
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()

按钮.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!")

游戏窗口管理器.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 )
4

1 回答 1

2

你使用的方式ButtonManager有点奇怪:当你调用时makeButtonself不是ButtonManager按照惯例应该是的实例,而是调用Frame,这非常违反直觉且过于复杂。另外,GameWindowManageris not a Frame,因此您不能在 中使用,等Frame属性和方法。packmaster__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()
于 2013-10-25T07:56:55.550 回答