fromPyQt5.QtWidgets import QApplication,QMainWindow,QActionfromPyQt5.QtGui import QIcon
import sysclass GUI(QMainWindow):#inherit fromQMainwindow
def __init__(self):
super().__init__()#initialize superclass,which creates the window
self.initGUI() #refer to windowasself
def initGUI(self):#add widgets and change properties
self.setWindowTitle("这不是周")#refer to window asself
self.resize(400,400) #resize(width,height)
self.add_menu_and_status()
def add_menu_and_status(self):
self.statusBar().showMessage("状态栏内容")
menubar=self.menuBar() #create menu bar
file_menu=menubar.addMenu("文件") #add menu to menu bar
new_icon=QIcon("file.png") #create icon
new_action=QAction(new_icon,"创建",self) #crteate an Action
new_action.setStatusTip("创建新文件")#statusbar pdated
file_menu.addAction(new_action) # add Action to menu
file_menu.addSeparator() #add separator line between menu
exit_icon= QIcon("exit.png") # create icon
exit_action= QAction(exit_icon, "退出", self) # crteate an Action
exit_action.setStatusTip("点击退出系统") # statusbar pdated
exit_action.triggered.connect(self.close)#close application when clicked
exit_action.setShortcut("Ctrl+Q") #keyboard shortcut to close application
file_menu.addAction(exit_action) # add Action to menu
#-------------------------------------edit_menu=menubar.addMenu("编辑")#add second menu to menu bar
self.resize(400,400)if __name__=="__main__":
app=QApplication(sys.argv) #create Application
gui=GUI() #create instance of classgui.show()#show the constructed Qt window
sys.exit(app.exec_())#execute the application