python closeevent_PyQt的QMainWindow closeEvent从未调用

closeEvent是QMainwindow的一种方法,但是您试图从自己的Ui_MainWindow类中使用它。请改为:from PyQt4 import QtCore, QtGui

class MainWindow(QtGui.QMainWindow):

def __init__(self, *args, **kwargs):

super(MainWindow, self).__init__(*args, **kwargs)

self.setupUi()

def setupUi(self):

self.setObjectName("MainWindow")

self.resize(277, 244)

self.statusbar = QtGui.QStatusBar()

self.statusbar.setObjectName("statusbar")

self.setStatusBar(self.statusbar)

def closeEvent(self,event):

result = QtGui.QMessageBox.question(self,

"Confirm Exit...",

"Are you sure you want to exit ?",

QtGui.QMessageBox.Yes| QtGui.QMessageBox.No)

event.ignore()

if result == QtGui.QMessageBox.Yes:

event.accept()

if __name__ == "__main__":

import sys

app = QtGui.QApplication(sys.argv)

mainWindow = MainWindow()

mainWindow.show()

sys.exit(app.exec_())

如果您希望尽可能多地保留结构,请尝试以下操作:from PyQt4 import QtCore, QtGui

class Ui_MainWindow(object):

def setupUi(self, MainWindow):

MainWindow.setObjectName("MainWindow")

MainWindow.resize(277, 244)

self.centralwidget = QtGui.QWidget(MainWindow)

self.centralwidget.setObjectName("centralwidget")

MainWindow.setCentralWidget(self.centralwidget)

self.statusbar = QtGui.QStatusBar(MainWindow)

self.statusbar.setObjectName("statusbar")

MainWindow.setStatusBar(self.statusbar)

QtCore.QMetaObject.connectSlotsByName(MainWindow)

MainWindow.show()

class MyWindow(QtGui.QMainWindow):

def closeEvent(self,event):

result = QtGui.QMessageBox.question(self,

"Confirm Exit...",

"Are you sure you want to exit ?",

QtGui.QMessageBox.Yes| QtGui.QMessageBox.No)

event.ignore()

if result == QtGui.QMessageBox.Yes:

event.accept()

if __name__ == "__main__":

import sys

app = QtGui.QApplication(sys.argv)

MainWindow = MyWindow()

ui = Ui_MainWindow()

ui.setupUi(MainWindow)

sys.exit(app.exec_())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用Python3+PyQT5+Pyserial实现简单的串口工具方法的示例代码: ``` python import sys import serial from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction, QFileDialog from PyQt5.QtCore import QThread, pyqtSignal class SerialThread(QThread): received = pyqtSignal(str) def __init__(self, port, baudrate): super().__init__() self.port = port self.baudrate = baudrate self.serial = None def run(self): try: self.serial = serial.Serial(self.port, self.baudrate) while True: data = self.serial.readline().decode("utf-8") if data: self.received.emit(data) except Exception as e: print(e) def write(self, data): if self.serial: self.serial.write(data.encode("utf-8")) def close(self): if self.serial: self.serial.close() class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Serial Tool") self.setGeometry(100, 100, 800, 600) self.text_edit = QTextEdit(self) self.text_edit.setReadOnly(True) self.setCentralWidget(self.text_edit) open_action = QAction("Open", self) open_action.setShortcut("Ctrl+O") open_action.triggered.connect(self.open_file) self.menuBar().addAction(open_action) self.thread = SerialThread("COM1", 115200) self.thread.received.connect(self.append_text) self.thread.start() def append_text(self, text): self.text_edit.moveCursor(QtGui.QTextCursor.End) self.text_edit.insertPlainText(text) def open_file(self): file_name, _ = QFileDialog.getOpenFileName(self, "Open File", "", "Text Files (*.txt)") if file_name: with open(file_name, "r") as f: data = f.read() self.thread.write(data) def closeEvent(self, event): self.thread.close() event.accept() if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 这个示例代码实现了一个简单的串口工具,可以实现打开串口、读取串口数据、发送数据等功能。其中SerialThread类用于在后台线程中读取串口数据,MainWindow类用于创建窗口并处理窗口事件。该代码可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值