Python 調用PyQt5 制作對話框,退出時候有二次確認(注:默認是直接退出)

1 # -*- ytf-8 -*- 2 """ 3 用PyQt建一個對話框,退出時提示有二次確認 4 """ 5 6 import sys 7 from PyQt5.QtWidgets import QApplication,QMessageBox,QWidget 8 9 class myWin(QWidget): 10 def __init__(self): 11 #執行父類的__init__構造方法 12 super().__init__() 13 #將窗口的設置委托給initUI方法 14 self.initUI() 15 def initUI(self): 16 #設置窗口 17 self.setWindowTitle("消息框") 18 self.setGeometry(200,200,500,500) #先位置再大小 19 self.show() 20 #重寫關閉事件方法(closeEvent) 21 def closeEvent(self,event): 22 #獲取消息框實例的值 23 msg = QMessageBox.question(self,"退出警告","你確定退出嗎?",QMessageBox.Yes|QMessageBox.No,QMessageBox.No) #這里是固定格式,yes/no不能動 24 #判斷消息的返回值 25 if msg ==QMessageBox.Yes: 26 event.accept() 27 else: 28 event.ignore() 29 if __name__=="__main__": 30 app=QApplication(sys.argv) 31 my=myWin() 32 sys.exit(app.exec_())
Python_menuBar

1 import sys 2 from PyQt5.QtWidgets import QApplication,QMainWindow,QAction,QMessageBox 3 from PyQt5.QtGui import QIcon 4 5 class myWin(QMainWindow): 6 def __init__(self): 7 super().__init__() 8 self.initUI() 9 10 def initUI(self): 11 #創建動作對象並設置 12 action_close=QAction(QIcon('icon.png'),'&退出',self) 13 action_close.setShortcut('A') 14 action_close.setStatusTip('這是退出功能') 15 action_close.triggered.connect(self.close) 16 17 action_open=QAction(QIcon('icon.png'),'&打開',self) 18 action_open.setShortcut('Ctrl+O') 19 action_open.setStatusTip('這是打開功能') 20 action_open.triggered.connect(self.open1) 21 22 #創建菜單 23 menuBar=self.menuBar() 24 menufile=menuBar.addMenu('&文件') 25 menufile.addAction(action_open) 26 menufile.addAction(action_close) 27 28 #設置狀態欄 29 self.statusBar().showMessage('這個是狀態欄....') 30 31 #設置窗口屬性 32 self.setGeometry(100,300,400,400) 33 self.setWindowTitle('菜單欄實例') 34 self.setWindowIcon(QIcon('icon.png')) 35 36 self.show() 37 #重寫關閉事件方法(closeEvent) 38 def closeEvent(self,event): 39 #獲取消息框實例的值 40 msg=QMessageBox.question(self,'退出警告','你確認退出嗎??',QMessageBox.Yes | QMessageBox.No,QMessageBox.No) 41 #判定消息框的返回值 42 if msg==QMessageBox.Yes: 43 event.accept() 44 else: 45 event.ignore() 46 def open1(self): 47 print('馬季是個相聲家') 48 49 if __name__=='__main__': 50 app=QApplication(sys.argv) 51 my=myWin() 52 sys.exit(app.exec_())

1 # -*- coding:utf-8 -*- 2 3 """ 4 圖形界面 5 拖動 只在圖形界面 6 7 8 對於接收拖動組件: 9 1、開啟 setAcceptDrops 允許接收拖動 10 2、重寫 dragEnterEvent 對拖入數據進行過濾 11 3、重寫 dropEvent 放入拖拽狀態 12 13 對於拖出組件 14 設置setDragEnabled 為 True 允許拖動操作 15 16 17 目標:將單行文本框里的文字拖到按鈕中,在按鈕中顯示 18 """ 19 20 from PyQt5.QtWidgets import QApplication,QWidget,QLineEdit,QPushButton 21 import sys 22 23 #創建一個可以拖入的按鍵類型 24 class btn_drag(QPushButton): 25 #注意構造方法與按鈕之間的參數的需求關系 26 def __init__(self,title,parent): #形參 27 super().__init__(title,parent) 28 self.setAcceptDrops(True) #允許拖入 29 30 def dragEnterEvent(self,e): #重寫 拖拽進入方法 31 if e.mimeData().hasFormat("text/plain"): #過濾信息,只能是文本下的純文本格式 32 e.accept() 33 else: 34 e.ignore() 35 36 def dropEvent(self,e): #重寫放入事件 37 self.setText(e.mimeData().text()) #將拖入的文本信息放入到當前實例的text中 38 39 #新建基本框,有單行文本框和按鈕 40 class myWin(QWidget): 41 def __init__(self): 42 super().__init__() 43 self.initUI() 44 45 def initUI(self): 46 btn=btn_drag("ok",self) #實例化框中按鈕 47 edit=QLineEdit('000',self) 48 edit.setDragEnabled(True) #設置允許拖拽 49 btn.setGeometry(10,10,100,40) 50 edit.setGeometry(10,70,80,20) 51 52 self.setGeometry(150,150,500,300) 53 edit.setWindowTitle("拖拽實例1") 54 self.show() 55 56 if __name__ == "__main__": 57 app = QApplication(sys.argv) 58 my = myWin() 59 sys.exit(app.exec_())

1 """ 2 選擇框 3 """ 4 5 import sys 6 from PyQt5.QtWidgets import QApplication,QWidget,QComboBox #combox選擇框 7 8 class myWin(QWidget): 9 def __init__(self): 10 super().__init__() 11 self.initUI() 12 def initUI(self): 13 self.com=QComboBox(self) #實例化選擇框 14 self.com.setGeometry(40,40,100,20) 15 self.com.addItem("東") 16 self.com.addItem("南") 17 self.com.addItem("西") 18 self.com.addItem("北") 19 20 self.setGeometry(100,100,300,300) 21 self.setWindowTitle("選擇框") 22 self.show() 23 def shows(self,date): 24 print(date) 25 self.lb1.setText(str(date)) 26 27 if __name__=="__main__": 28 app = QApplication(sys.argv) 29 my = myWin() 30 sys.exit(app.exec_()) 31