我想用pyqt5仿写一个记事本,界面是通过qtdesigner做了之后转成python的,为了实现界面与业务分离,在另外一个python文件中写业务代码。
问题是我想在点击文件->新建之后能在文本框里显示一些文本。在业务代码中连接了信号与槽,但是完全不起作用,点了一点反应也没有,大佬们知道哪里出错了吗?
以下是完整代码:
notepad.py
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from notepadui import Ui_MainWindow
class MyNotebook(QMainWindow, Ui_MainWindow):#定义新的类实现界面与操作代码的分离
resized = QtCore.pyqtSignal()#定义一个新的信号
def __init__(self, parent=None):
super(MyNotebook, self).__init__(parent=parent)#父类初始化
self.setupUi(self)
self.action.triggered.connect(self.on_newFile)
self.resized.connect(self.adjustTextEditSize)
self.setting()
def resizeEvent(self, event):#重写调整窗口尺寸函数
self.resized.emit()#发射信号
return super(MyNotebook, self).resizeEvent(event)
def setting(self):
pass
def on_newFile(self):
self.textEdit.setText('new')#始终执行不到这里的槽
def adjustTextEditSize(self):
self.textEdit.resize(self.width, self.height-23)#调整文本框大小与窗口大小一致
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = MyNotebook()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
notepadui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'notepad.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(667, 545)
font = QtGui.QFont()
font.setFamily("幼圆")
MainWindow.setFont(font)
MainWindow.setAutoFillBackground(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(0, 0, 667, 522))
self.textEdit.setObjectName("textEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 667, 18))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
self.menu_2 = QtWidgets.QMenu(self.menubar)
self.menu_2.setObjectName("menu_2")
self.menu_3 = QtWidgets.QMenu(self.menubar)
self.menu_3.setObjectName("menu_3")
self.menu_4 = QtWidgets.QMenu(self.menubar)
self.menu_4.setObjectName("menu_4")
self.menu_6 = QtWidgets.QMenu(self.menu_4)
self.menu_6.setObjectName("menu_6")
self.menu_5 = QtWidgets.QMenu(self.menubar)
self.menu_5.setObjectName("menu_5")
MainWindow.setMenuBar(self.menubar)
self.action = QtWidgets.QAction(MainWindow)
self.action.setObjectName("action")
self.action_2 = QtWidgets.QAction(MainWindow)
self.action_2.setObjectName("action_2")
self.action_3 = QtWidgets.QAction(MainWindow)
self.action_3.setObjectName("action_3")
self.action_4 = QtWidgets.QAction(MainWindow)
self.action_4.setObjectName("action_4")
self.action_5 = QtWidgets.QAction(MainWindow)
self.action_5.setObjectName("action_5")
self.action_7 = QtWidgets.QAction(MainWindow)
self.action_7.setObjectName("action_7")
self.action_8 = QtWidgets.QAction(MainWindow)
self.action_8.setObjectName("action_8")
self.action_10 = QtWidgets.QAction(MainWindow)
self.action_10.setObjectName("action_10")
self.action_11 = QtWidgets.QAction(MainWindow)
self.action_11.setObjectName("action_11")
self.action_13 = QtWidgets.QAction(MainWindow)
self.action_13.setObjectName("action_13")
self.action_14 = QtWidgets.QAction(MainWindow)
self.action_14.setObjectName("action_14")
self.action_15 = QtWidgets.QAction(MainWindow)
self.action_15.setObjectName("action_15")
self.action_16 = QtWidgets.QAction(MainWindow)
self.action_16.setObjectName("action_16")
self.action_18 = QtWidgets.QAction(MainWindow)
self.action_18.setObjectName("action_18")
self.action_19 = QtWidgets.QAction(MainWindow)
self.action_19.setObjectName("action_19")
self.action_20 = QtWidgets.QAction(MainWindow)
self.action_20.setObjectName("action_20")
self.action_21 = QtWidgets.QAction(MainWindow)
self.action_21.setObjectName("action_21")
self.action_22 = QtWidgets.QAction(MainWindow)
self.action_22.setObjectName("action_22")
self.action_24 = QtWidgets.QAction(MainWindow)
self.action_24.setObjectName("action_24")
self.action_25 = QtWidgets.QAction(MainWindow)
self.action_25.setObjectName("action_25")
self.action_26 = QtWidgets.QAction(MainWindow)
self.action_26.setCheckable(True)
self.action_26.setObjectName("action_26")
self.action_27 = QtWidgets.QAction(MainWindow)
self.action_27.setObjectName("action_27")
self.action_29 = QtWidgets.QAction(MainWindow)
self.action_29.setCheckable(True)
self.action_29.setObjectName("action_29")
self.action_30 = QtWidgets.QAction(MainWindow)
self.action_30.setObjectName("action_30")
self.action_31 = QtWidgets.QAction(MainWindow)
self.action_31.setObjectName("action_31")
self.action_33 = QtWidgets.QAction(MainWindow)
self.action_33.setObjectName("action_33")
self.action_34 = QtWidgets.QAction(MainWindow)
self.action_34.setObjectName("action_34")
self.action_35 = QtWidgets.QAction(MainWindow)
self.action_35.setObjectName("action_35")
self.action_36 = QtWidgets.QAction(MainWindow)
self.action_36.setObjectName("action_36")
self.menu.addAction(self.action)
self.menu.addAction(self.action_2)
self.menu.addAction(self.action_3)
self.menu.addAction(self.action_4)
self.menu.addAction(self.action_5)
self.menu.addSeparator()
self.menu.addAction(self.action_7)
self.menu.addAction(self.action_8)
self.menu.addSeparator()
self.menu.addAction(self.action_10)
self.menu_2.addAction(self.action_11)
self.menu_2.addSeparator()
self.menu_2.addAction(self.action_13)
self.menu_2.addAction(self.action_14)
self.menu_2.addAction(self.action_15)
self.menu_2.addAction(self.action_16)
self.menu_2.addSeparator()
self.menu_2.addAction(self.action_18)
self.menu_2.addAction(self.action_19)
self.menu_2.addAction(self.action_20)
self.menu_2.addAction(self.action_21)
self.menu_2.addAction(self.action_22)
self.menu_2.addSeparator()
self.menu_2.addAction(self.action_24)
self.menu_2.addAction(self.action_25)
self.menu_3.addAction(self.action_26)
self.menu_3.addAction(self.action_27)
self.menu_6.addAction(self.action_34)
self.menu_6.addAction(self.action_35)
self.menu_6.addAction(self.action_36)
self.menu_4.addAction(self.menu_6.menuAction())
self.menu_4.addAction(self.action_29)
self.menu_5.addAction(self.action_30)
self.menu_5.addAction(self.action_31)
self.menu_5.addSeparator()
self.menu_5.addAction(self.action_33)
self.menubar.addAction(self.menu.menuAction())
self.menubar.addAction(self.menu_2.menuAction())
self.menubar.addAction(self.menu_3.menuAction())
self.menubar.addAction(self.menu_4.menuAction())
self.menubar.addAction(self.menu_5.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Notepad"))
self.menu.setTitle(_translate("MainWindow", "文件"))
self.menu_2.setTitle(_translate("MainWindow", "编辑"))
self.menu_3.setTitle(_translate("MainWindow", "格式"))
self.menu_4.setTitle(_translate("MainWindow", "查看"))
self.menu_6.setTitle(_translate("MainWindow", "缩放"))
self.menu_5.setTitle(_translate("MainWindow", "帮助"))
self.action.setText(_translate("MainWindow", "新建"))
self.action.setShortcut(_translate("MainWindow", "Ctrl+N"))
self.action_2.setText(_translate("MainWindow", "新窗口"))
self.action_2.setShortcut(_translate("MainWindow", "Ctrl+Shift+N"))
self.action_3.setText(_translate("MainWindow", "打开"))
self.action_3.setShortcut(_translate("MainWindow", "Ctrl+O"))
self.action_4.setText(_translate("MainWindow", "保存"))
self.action_4.setShortcut(_translate("MainWindow", "Ctrl+S"))
self.action_5.setText(_translate("MainWindow", "另存为"))
self.action_5.setShortcut(_translate("MainWindow", "Ctrl+Shift+S"))
self.action_7.setText(_translate("MainWindow", "页面设置"))
self.action_8.setText(_translate("MainWindow", "打印"))
self.action_8.setShortcut(_translate("MainWindow", "Ctrl+P"))
self.action_10.setText(_translate("MainWindow", "退出"))
self.action_11.setText(_translate("MainWindow", "撤销"))
self.action_11.setShortcut(_translate("MainWindow", "Ctrl+Z"))
self.action_13.setText(_translate("MainWindow", "剪切"))
self.action_13.setShortcut(_translate("MainWindow", "Ctrl+X"))
self.action_14.setText(_translate("MainWindow", "复制"))
self.action_14.setShortcut(_translate("MainWindow", "Ctrl+C"))
self.action_15.setText(_translate("MainWindow", "粘贴"))
self.action_15.setShortcut(_translate("MainWindow", "Ctrl+V"))
self.action_16.setText(_translate("MainWindow", "删除"))
self.action_16.setShortcut(_translate("MainWindow", "Del"))
self.action_18.setText(_translate("MainWindow", "查找"))
self.action_18.setShortcut(_translate("MainWindow", "Ctrl+F"))
self.action_19.setText(_translate("MainWindow", "查找下一个"))
self.action_19.setShortcut(_translate("MainWindow", "F3"))
self.action_20.setText(_translate("MainWindow", "查找上一个"))
self.action_20.setShortcut(_translate("MainWindow", "Shift+F3"))
self.action_21.setText(_translate("MainWindow", "替换"))
self.action_21.setShortcut(_translate("MainWindow", "Ctrl+H"))
self.action_22.setText(_translate("MainWindow", "转到"))
self.action_22.setShortcut(_translate("MainWindow", "Ctrl+G"))
self.action_24.setText(_translate("MainWindow", "全选"))
self.action_24.setShortcut(_translate("MainWindow", "Ctrl+A"))
self.action_25.setText(_translate("MainWindow", "时间/日期"))
self.action_25.setShortcut(_translate("MainWindow", "F5"))
self.action_26.setText(_translate("MainWindow", "自动换行"))
self.action_27.setText(_translate("MainWindow", "字体"))
self.action_29.setText(_translate("MainWindow", "状态栏"))
self.action_30.setText(_translate("MainWindow", "查看帮助"))
self.action_31.setText(_translate("MainWindow", "发送反馈"))
self.action_33.setText(_translate("MainWindow", "关于记事本"))
self.action_34.setText(_translate("MainWindow", "放大"))
self.action_34.setShortcut(_translate("MainWindow", "Ctrl++"))
self.action_35.setText(_translate("MainWindow", "缩小"))
self.action_35.setShortcut(_translate("MainWindow", "Ctrl+-"))
self.action_36.setText(_translate("MainWindow", "恢复默认缩放"))
self.action_36.setShortcut(_translate("MainWindow", "Ctrl+0"))