Python 调用PyQt5 制作对话框,退出时候有二次确认(注:默认是直接退出)


1 1 # -*- ytf-8 -*- 2 2 """ 3 3 用PyQt建一个对话框,退出时提示有二次确认 4 4 """ 5 5 6 6 import sys 7 7 from PyQt5.QtWidgets import QApplication,QMessageBox,QWidget 8 8 9 9 class myWin(QWidget): 1010 def __init__(self): 1111 #执行父类的__init__构造方法 1212 super().__init__() 1313 #将窗口的设置委托给initUI方法 1414 self.initUI() 1515 def initUI(self): 1616 #设置窗口 1717 self.setWindowTitle("消息框") 1818 self.setGeometry(200,200,500,500) #先位置再大小 1919 self.show() 2020 #重写关闭事件方法(closeEvent) 2121 def closeEvent(self,event): 2222 #获取消息框实例的值 2323 msg = QMessageBox.question(self,"退出警告","你确定退出吗?",QMessageBox.Yes|QMessageBox.No,QMessageBox.No) #这里是固定格式,yes/no不能动 2424 #判断消息的返回值 2525 if msg ==QMessageBox.Yes: 2626 event.accept() 2727 else: 2828 event.ignore() 2929 if __name__=="__main__": 3030 app=QApplication(sys.argv) 3131 my=myWin() 3232 sys.exit(app.exec_())
PyQt5_对话框_退出时确认
Python_menuBar


1 1 import sys 2 2 from PyQt5.QtWidgets import QApplication,QMainWindow,QAction,QMessageBox 3 3 from PyQt5.QtGui import QIcon 4 4 5 5 class myWin(QMainWindow): 6 6 def __init__(self): 7 7 super().__init__() 8 8 self.initUI() 9 9 1010 def initUI(self): 1111 #创建动作对象并设置 1212 action_close=QAction(QIcon('icon.png'),'&退出',self) 1313 action_close.setShortcut('A') 1414 action_close.setStatusTip('这是退出功能') 1515 action_close.triggered.connect(self.close) 1616 1717 action_open=QAction(QIcon('icon.png'),'&打开',self) 1818 action_open.setShortcut('Ctrl+O') 1919 action_open.setStatusTip('这是打开功能') 2020 action_open.triggered.connect(self.open1) 2121 2222 #创建菜单 2323 menuBar=self.menuBar() 2424 menufile=menuBar.addMenu('&文件') 2525 menufile.addAction(action_open) 2626 menufile.addAction(action_close) 2727 2828 #设置状态栏 2929 self.statusBar().showMessage('这个是状态栏....') 3030 3131 #设置窗口属性 3232 self.setGeometry(100,300,400,400) 3333 self.setWindowTitle('菜单栏实例') 3434 self.setWindowIcon(QIcon('icon.png')) 3535 3636 self.show() 3737 #重写关闭事件方法(closeEvent) 3838 def closeEvent(self,event): 3939 #获取消息框实例的值 4040 msg=QMessageBox.question(self,'退出警告','你确认退出吗??',QMessageBox.Yes | QMessageBox.No,QMessageBox.No) 4141 #判定消息框的返回值 4242 if msg==QMessageBox.Yes: 4343 event.accept() 4444 else: 4545 event.ignore() 4646 def open1(self): 4747 print('马季是个相声家') 4848 4949 if __name__=='__main__': 5050 app=QApplication(sys.argv) 5151 my=myWin() 5252 sys.exit(app.exec_())
Python_menuBar


1 1 # -*- coding:utf-8 -*- 2 2 3 3 """ 4 4 图形界面 5 5 拖动 只在图形界面 6 6 7 7 8 8 对于接收拖动组件: 9 9 1、开启 setAcceptDrops 允许接收拖动 1010 2、重写 dragEnterEvent 对拖入数据进行过滤 1111 3、重写 dropEvent 放入拖拽状态 1212 1313 对于拖出组件 1414 设置setDragEnabled 为 True 允许拖动操作 1515 1616 1717 目标:将单行文本框里的文字拖到按钮中,在按钮中显示 1818 """ 1919 2020 from PyQt5.QtWidgets import QApplication,QWidget,QLineEdit,QPushButton 2121 import sys 2222 2323 #创建一个可以拖入的按键类型 2424 class btn_drag(QPushButton): 2525 #注意构造方法与按钮之间的参数的需求关系 2626 def __init__(self,title,parent): #形参 2727 super().__init__(title,parent) 2828 self.setAcceptDrops(True) #允许拖入 2929 3030 def dragEnterEvent(self,e): #重写 拖拽进入方法 3131 if e.mimeData().hasFormat("text/plain"): #过滤信息,只能是文本下的纯文本格式 3232 e.accept() 3333 else: 3434 e.ignore() 3535 3636 def dropEvent(self,e): #重写放入事件 3737 self.setText(e.mimeData().text()) #将拖入的文本信息放入到当前实例的text中 3838 3939 #新建基本框,有单行文本框和按钮 4040 class myWin(QWidget): 4141 def __init__(self): 4242 super().__init__() 4343 self.initUI() 4444 4545 def initUI(self): 4646 btn=btn_drag("ok",self) #实例化框中按钮 4747 edit=QLineEdit('000',self) 4848 edit.setDragEnabled(True) #设置允许拖拽 4949 btn.setGeometry(10,10,100,40) 5050 edit.setGeometry(10,70,80,20) 5151 5252 self.setGeometry(150,150,500,300) 5353 edit.setWindowTitle("拖拽实例1") 5454 self.show() 5555 5656 if __name__ == "__main__": 5757 app = QApplication(sys.argv) 5858 my = myWin() 5959 sys.exit(app.exec_())
PyQt5 拖动单行文本框的文字到一个按钮里


1 1 """ 2 2 选择框 3 3 """ 4 4 5 5 import sys 6 6 from PyQt5.QtWidgets import QApplication,QWidget,QComboBox #combox选择框 7 7 8 8 class myWin(QWidget): 9 9 def __init__(self): 1010 super().__init__() 1111 self.initUI() 1212 def initUI(self): 1313 self.com=QComboBox(self) #实例化选择框 1414 self.com.setGeometry(40,40,100,20) 1515 self.com.addItem("东") 1616 self.com.addItem("南") 1717 self.com.addItem("西") 1818 self.com.addItem("北") 1919 2020 self.setGeometry(100,100,300,300) 2121 self.setWindowTitle("选择框") 2222 self.show() 2323 def shows(self,date): 2424 print(date) 2525 self.lb1.setText(str(date)) 2626 2727 if __name__=="__main__": 2828 app = QApplication(sys.argv) 2929 my = myWin() 3030 sys.exit(app.exec_()) 3131
选择框