PyQt5+designer实现--------python

首先在desinger绘制完成界面后,转换为ui文件,之后
在最下面写上如下代码运行即可显示界面。

if __name__ == "__main__":
	from PyQt5.Qt import *
	import requests
	import sys
    app = QApplication(sys.argv)
    mainwindow = QWidget()
    ui = Ui_Dialog()
    ui.setupUi(mainwindow)
    mainwindow.show()
    sys.exit(app.exec_())

当要写按钮点击事件时,先在Ui_Dialog类中定义一个函数

##这是自己定义的一个函数
def translation(self):
	##代码

之后在setupUi函数中写

#括号里写的是自己定义的函数名
self.pushButton.clicked.connect(self.translation)

即可创建相关按钮点击事件

全部代码如下:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
import requests
import sys


class Ui_Dialog(QMainWindow):
    def __init__(self):
        print('123')
        super(Ui_Dialog,self).__init__()
        print('4')
        self.setupUi(self)
        self.retranslateUi(self)
        print(self)
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(478, 400)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(40, 30, 141, 31))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(50, 190, 141, 31))
        self.label_2.setObjectName("label_2")
        self.textEdit = QtWidgets.QTextEdit(Dialog)
        self.textEdit.setGeometry(QtCore.QRect(50, 240, 361, 101))
        self.textEdit.setObjectName("textEdit")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(340, 190, 71, 31))
        self.pushButton.setObjectName("pushButton")
        self.textEdit_2 = QtWidgets.QTextEdit(Dialog)
        self.textEdit_2.setGeometry(QtCore.QRect(53, 70, 361, 101))
        self.textEdit_2.setObjectName("textEdit_2")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
        self.pushButton.clicked.connect(self.translation)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "翻译器"))
        self.label.setText(_translate("Dialog", "请输入要翻译的内容:"))
        self.label_2.setText(_translate("Dialog", "翻译结果:"))
        self.pushButton.setText(_translate("Dialog", "翻译"))

    def translation(self):
        post_url = 'https://fanyi.baidu.com/sug'
        header = {
     
            'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36x-requested-with: XMLHttpRequest'
        }
        word = self.textEdit_2.toPlainText()
        data = {
     
            'kw':word
        }
        response = requests.post(url=post_url,data=data,headers=header).json()
        response_list = response['data']
        print(response_list)
        response_data = str(response_list)
        self.textEdit.setText(response_data)



if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainwindow = QWidget()
    ui = Ui_Dialog()
    ui.setupUi(mainwindow)
    mainwindow.show()
    sys.exit(app.exec_())

你可能感兴趣的:(Python学习笔记,python,pyqt)