import sys from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QMainWindow, QMenuBar, QToolBar, QTextEdit, QAction, QApplication, qApp, QMessageBox from PyQt5.QtCore import Qt import threading import time songs = ['愛情買賣','朋友','回家過年','好日子'] films = ['阿凡達','猩球崛起'] class MyWindow(QMainWindow): def __init__(self): super().__init__() self.resize(677, 442) self.setWindowTitle("我的程序") self.createUI() self.createAction() self.createStatusbar() self.createMenu() self.createToolbar() def createUI(self): self.textedit = QTextEdit() self.setCentralWidget(self.textedit) # 動作 def createAction(self): self.exit_action = QAction(QIcon("ico_new.jpg"), "退出", self, triggered=qApp.quit) self.exit_action.setStatusTip("退出程序") self.exit_action.setShortcut("Ctrl+Q") self.exit_action.triggered.connect(qApp.quit) # 狀態欄 def createStatusbar(self): self.statusBar() # 菜單欄 def createMenu(self): #menubar = QMenuBar(self) menubar = self.menuBar() menu = menubar.addMenu("文件(F)") menu.addAction(QAction(QIcon("ico_new_16_16.jpg"), "新建", self, triggered=qApp.quit)) # 帶圖標,文字 menu.addAction(QAction(QIcon("ico_open_16_16.jpg"), "打開", self, triggered=qApp.quit)) menu.addAction(QAction(QIcon("ico_save_16_16.jpg"), "保存", self, triggered=qApp.quit)) menu.addSeparator() menu.addAction(QAction(QIcon("ico_close_16_16.jpg"), "關閉", self, triggered=lambda :QMessageBox.about(self, '關閉','關閉。。。'))) menu = menubar.addMenu("編輯(E)") menu.addAction(QAction("撤銷", self, triggered=qApp.quit)) # 不帶圖標 menu.addAction(QAction("剪切", self, triggered=qApp.quit)) menu.addAction(QAction("復制", self, triggered=qApp.quit)) menu.addAction(QAction("粘貼", self, triggered=qApp.quit)) menu = menubar.addMenu("娛樂(S)") menu.addAction(QAction("音樂", self, triggered=lambda :self.thread_it(self.music, songs))) # 線程 menu.addAction(QAction("電影", self, triggered=lambda :self.thread_it(self.movie, films))) menu = menubar.addMenu("幫助(H)") menu.addAction('&New', lambda :QMessageBox.about(self, 'New','新建。。。'), Qt.CTRL + Qt.Key_N) # 注意快捷鍵 menu.addAction('關於', lambda :QMessageBox.about(self, '關於','關於。。。'), Qt.CTRL + Qt.Key_Q) # 工具欄 def createToolbar(self): toolbar = self.addToolBar('文件') toolbar.addAction(QAction(QIcon("ico_new_16_16.jpg"), "新建", self, triggered=qApp.quit)) # 帶圖標,文字 toolbar.addAction(QAction(QIcon("ico_open_16_16.jpg"), "打開", self, triggered=qApp.quit)) toolbar.addSeparator() toolbar.addAction(QAction(QIcon("ico_save_16_16.jpg"), "打開", self, triggered=qApp.quit)) toolbar = self.addToolBar("編輯") toolbar.addAction(QAction("撤銷", self, triggered=qApp.quit)) # 不帶圖標 toolbar.addAction(QAction("剪切", self, triggered=qApp.quit)) # 邏輯:聽音樂 def music(self, songs): for x in songs: self.textedit.append("聽音樂:%s \t-- %s" %(x, time.ctime())) time.sleep(3) # 邏輯:看電影 def movie(self, films): for x in films: self.textedit.append("看電影:%s \t-- %s" %(x, time.ctime())) time.sleep(5) # 打包進線程(耗時的操作) @staticmethod def thread_it(func, *args): t = threading.Thread(target=func, args=args) t.setDaemon(True) # 守護--就算主界面關閉,線程也會留守后台運行(不對!) t.start() # 啟動 # t.join() # 阻塞--會卡死界面! app = QApplication(sys.argv) win = MyWindow() win.show() sys.exit(app.exec_())