pyqt使用命令记录
程序员文章站
2022-05-11 08:04:48
...
- 首先打开qt designer,安装过程不赘诉,在安装完毕并添加到命令行之后。命令行输入。
.designer.exe
- 在qt designer新建ui文件并保存。
- 将ui文件编译成所需要的py文件
pyuic5 -o OnlineUpdateUI.py Online-Update.ui
- 为了方便今后ui文件的添加与修改,不推荐直接在生成的.py文件上进行开发,这里使用继承的方式来降低耦合性,在一定程度上实现分离。
这是我生成的OnlineUpdateUI.py文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Online-Update.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(406, 343)
self.UpdateButton = QtWidgets.QPushButton(Form)
self.UpdateButton.setGeometry(QtCore.QRect(260, 320, 131, 21))
self.UpdateButton.setObjectName("UpdateButton")
self.showup_textEdit = QtWidgets.QTextEdit(Form)
self.showup_textEdit.setGeometry(QtCore.QRect(10, 70, 381, 241))
self.showup_textEdit.setObjectName("showup_textEdit")
self.NodeIPEdit = QtWidgets.QLineEdit(Form)
self.NodeIPEdit.setGeometry(QtCore.QRect(250, 10, 131, 20))
self.NodeIPEdit.setObjectName("NodeIPEdit")
self.Node_IP = QtWidgets.QLabel(Form)
self.Node_IP.setGeometry(QtCore.QRect(200, 10, 51, 21))
self.Node_IP.setObjectName("Node_IP")
self.local_IP = QtWidgets.QLabel(Form)
self.local_IP.setGeometry(QtCore.QRect(10, 10, 51, 21))
self.local_IP.setObjectName("local_IP")
self.LocalIP_Edit = QtWidgets.QLineEdit(Form)
self.LocalIP_Edit.setGeometry(QtCore.QRect(60, 10, 131, 20))
self.LocalIP_Edit.setObjectName("LocalIP_Edit")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(10, 40, 54, 12))
self.label.setObjectName("label")
self.FileNamelineEdit = QtWidgets.QLineEdit(Form)
self.FileNamelineEdit.setGeometry(QtCore.QRect(60, 40, 321, 20))
self.FileNamelineEdit.setObjectName("FileNamelineEdit")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.UpdateButton.setText(_translate("Form", "更新"))
self.Node_IP.setText(_translate("Form", "节点IP:"))
self.local_IP.setText(_translate("Form", "本机IP:"))
self.label.setText(_translate("Form", "文件名:"))
- main.py
这是main.py文件的模板,未全部上传代码,不过可以很清楚的看到槽函数的连接过程,删除不需要的一些代码之后可以直接运行。
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from OnlineUpdateUI import Ui_Form
class Demo(QWidget, Ui_Form):
def __init__(self):
super(Demo, self).__init__()
self.setupUi(self) # 1
#self.text_edit.textChanged.connect(self.show_text_func) # 2
self.UpdateButton.clicked.connect(self.update_button_func)
self.workThread = WorkThread()
self.workThread.getreturn.connect(self.showmessage)
#设置默认的IP
self.LocalIP_Edit.setText(self.workThread.master_ip)
self.NodeIPEdit.setText(self.workThread.host_ip)
self.FileNamelineEdit.setText(self.workThread.filename)
self.showup_textEdit.append("操作步骤:")
self.showup_textEdit.append("1. 将编译好的文件放入所在FTP文件夹")
self.showup_textEdit.append("2. 填写好IP及文件夹名称(不填则使用默认IP及文件名)")
def showmessage(self,message):
self.showup_textEdit.append(message)
def update_button_func(self):
print("button pressed!")
str_localIP = self.LocalIP_Edit.text()
str_nodeIP = self.NodeIPEdit.text()
str_filename = self.FileNamelineEdit.text()
self.workThread.master_ip = str_localIP
self.workThread.host_ip = str_nodeIP
self.workThread.filename = str_filename
print("本机IP和节点IP分别为:",str_localIP,str_nodeIP)
self.showup_textEdit.append("你好主机! IP:" + str_localIP)
self.showup_textEdit.append("你好节点! IP:" + str_nodeIP)
self.workThread.start()
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())