我一直试图将这些方法分成两个类,其中一个叫做
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()