Py学习  »  Python

在python中,如何将一个类的一些方法划分为两个类

Thomas • 2 年前 • 242 次点击  

我一直试图将这些方法分成两个类,其中一个叫做 menu 包含菜单栏的样式,另一个称为 commands ,我想包含菜单栏的命令。到目前为止,我有一些想法,但没有一个有效,我也在研究tk。我还在想办法继承遗产。以下是代码:

代码:

class Menubar(Frame): #Class for the menubar

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self): #That method creates the menubar and submenu!

        menubar = Menu(self.master)
        self.master.config(menu=menubar)

        fileMenu = Menu(menubar, tearoff=0)

        submenu = Menu(fileMenu, tearoff=0)
        submenu.add_command(label="Καταχώρηση νέου Πάρκου", command=Command.newPark())
        submenu.add_command(label="Επεξεργασία Πάρκου", command=Command.editPark())
        fileMenu.add_cascade(label='Πάρκα', menu=submenu, underline=0)

        fileMenu.add_separator()

        fileMenu.add_command(label="Έξοδος", underline=0, command=Command.onExit(self))
        menubar.add_cascade(label="Αρχείο", underline=0, menu=fileMenu)

        viewMenu = Menu(menubar, tearoff=0)
        
        submenu = Menu(viewMenu, tearoff=0)
        submenu.add_command(label="Όλα", command=self.viewAllParks)
        submenu.add_command(label="Όσα έχουν WiFi", command=self.viewOnlyWiFiParks)
        submenu.add_command(label="Όσα δεν έχουν WiFi", command=self.viewParksWithoutWiFi)
        viewMenu.add_cascade(label="Προβολή Παρκών", menu=submenu)
        
        viewMenu.add_separator()

        viewMenu.add_command(label="Logs Πάρκου", underline=0, command=self.logs)
        menubar.add_cascade(label="Προβολή", underline=0, menu=viewMenu)
        
        toolsMenu = Menu(menubar, tearoff=0)
        
        toolsMenu.add_command(label="Βλάβες - Συντηρήσεις", command=self.errors)
        toolsMenu.add_command(label="Ανατολή - Δύση", command=self.cordinates)
        

        menubar.add_cascade(label="Εργαλεία", underline=1, menu=toolsMenu)

我来自希腊,所以忽略我作为菜单栏标签的不同语言字符串。

► 使现代化 :

我取得的一个小小突破是,我成功地将自己分到了一个班级,但现在的主要问题是,它同时打开了所有的窗口!我该怎么修?

class Commands(Menu):
    
    def __init__(self):
        super().__init__()
    
    def onExit(self):

        self.quit()
        
    def newPark():
        fileWin = Toplevel(window)
        winWidth = fileWin.winfo_reqwidth()
        winwHeight = fileWin.winfo_reqheight()
        posRight = int(fileWin.winfo_screenwidth() / 2 - winWidth / 2)
        posDown = int(fileWin.winfo_screenheight() / 2 - winwHeight / 2)
        fileWin.geometry("+{}+{}".format(posRight, posDown))
        
    def editPark():
        fileWin = Toplevel(window)
        winWidth = fileWin.winfo_reqwidth()
        winwHeight = fileWin.winfo_reqheight()
        posRight = int(fileWin.winfo_screenwidth() / 2 - winWidth / 2)
        posDown = int(fileWin.winfo_screenheight() / 2 - winwHeight / 2)
        fileWin.geometry("+{}+{}".format(posRight, posDown))
        
    def viewAllParks():
        pass
        
    def viewOnlyWiFiParks():
        pass
        
    def viewParksWithoutWiFi():
        pass
    
    def errors():
        toolsWin = Toplevel(window)
        winWidth = toolsWin.winfo_reqwidth()
        winwHeight = toolsWin.winfo_reqheight()
        posRight = int(toolsWin.winfo_screenwidth() / 2 - winWidth / 2)
        posDown = int(toolsWin.winfo_screenheight() / 2 - winwHeight / 2)
        toolsWin.geometry("+{}+{}".format(posRight, posDown))
    
    def cordinates():
        toolsWin = Toplevel(window)
        winWidth = toolsWin.winfo_reqwidth()
        winwHeight = toolsWin.winfo_reqheight()
        posRight = int(toolsWin.winfo_screenwidth() / 2 - winWidth / 2)
        posDown = int(toolsWin.winfo_screenheight() / 2 - winwHeight / 2)
        toolsWin.geometry("+{}+{}".format(posRight, posDown))

下面的代码是创建GUI窗口并运行代码的主要功能:

def main():
    global window
    
    window = Tk()

    # set window title
    window.title("Open the Websites")
    window.state("zoomed")
    # set window background color
    window.configure(bg='lightgray')
    menubar = Menubar()

if __name__ == '__main__':
    main()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128945
 
242 次点击