pyuic5 -o mainwin.ui ui_mainwin.py
ui_mainwin.py代码都是自动生成的,大家可以不需要动,在生成的文件中有一个Ui_MainWindow类。新的py文件main.py,代码如下:#coding = utf-8import sysfrom PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialogfrom Ui_login import Ui_MainWindow
class MyWin(QMainWindow, Ui_MainWindow): ''' 从QMainWindow, Ui_MainWindow继承,实现界面设计和业务实现的解耦 ''' def __init__(self, parent=None): super(MyWin, self).__init__(parent) self.setupUi(self) self.__setting() def __setting(self): ''' 增加自定义配置 ''' self.setWindowTitle("远程维护工具") self.resize(800, 550) self.splitter.setStretchFactor(0, 1) self.splitter.setStretchFactor(1, 3) self.splitter.setStretchFactor(2, 2) self.splitter.setMaximumWidth(1500)
#增加信号处理 self.pushButton.clicked.connect(self.slot1) self.actionClose.triggered.connect(self.close) self.actionOpen.triggered.connect(self.on_open) self.actionSave.triggered.connect(self.on_save)
#自定义信号处理函数 def slot1(self): print("点击。。。")
def on_save(self): FullFileName, _ = QFileDialog.getSaveFileName(self, '文件另存为', r'./', 'TXT (*.txt)') set_text = self.txt_view.toPlainText() with open(FullFileName, 'wt') as f: print(set_text, file=f)
def on_open(self): txtstr = "" FullFileName, _ = QFileDialog.getOpenFileName(self, '打开', r'./', 'TXT (*.txt)') with open(FullFileName, 'rt') as f: lines = f.readlines() for line in lines: txtstr = txtstr+line self.txt_view.setText(txtstr)
if __name__ == "__main__": app = QApplication(sys.argv) w = MyWin() w.show() sys.exit(app.exec_())