Python qt5-- 按钮点击事件控制和处理
程序员文章站
2022-05-21 11:56:56
Python qt5-- 按钮点击事件Python qt5-- 按钮点击事件Python3.6 Ubuntu1604-pyQt使用(不使用Qtdesigner)基本组件—布局—按钮事件处理...
Python qt5-- 按钮点击事件
Python3.6 Ubuntu1604-pyQt使用(不使用Qtdesigner)基本组件—布局—按钮事件处理
PyQt5 +python3.6+qt designer弄个按钮事件
笔者想建立10个对象的随机排列,gui如的界面如下图所示,通过qt5 designer设计如下,保存为ui文件。
按钮点击事件
首先通过点击paixu按钮就可以在文本框中输出排序结果。
将qt5保存的ui文件通过vs code转换为Python文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '/home/li/桌面/python/qt/suijipailie.ui'
#
# Created by: PyQt5 UI code generator 5.12
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(588, 467)
font = QtGui.QFont()
font.setFamily("URW Gothic L")
font.setBold(True)
font.setWeight(75)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.duixiang1 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang1.setGeometry(QtCore.QRect(10, 70, 101, 70))
self.duixiang1.setObjectName("duixiang1")
self.duixiang2 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang2.setGeometry(QtCore.QRect(130, 70, 104, 70))
self.duixiang2.setObjectName("duixiang2")
self.duixiang3 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang3.setGeometry(QtCore.QRect(250, 70, 104, 70))
self.duixiang3.setObjectName("duixiang3")
self.duixiang4 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang4.setGeometry(QtCore.QRect(360, 70, 104, 70))
self.duixiang4.setObjectName("duixiang4")
self.duixiang5 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang5.setGeometry(QtCore.QRect(470, 70, 104, 70))
self.duixiang5.setObjectName("duixiang5")
self.duixiang6 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang6.setGeometry(QtCore.QRect(10, 170, 104, 70))
self.duixiang6.setObjectName("duixiang6")
self.duixiang7 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang7.setGeometry(QtCore.QRect(130, 170, 104, 70))
self.duixiang7.setObjectName("duixiang7")
self.duixiang8 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang8.setGeometry(QtCore.QRect(250, 170, 104, 70))
self.duixiang8.setObjectName("duixiang8")
self.duixiang9 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang9.setGeometry(QtCore.QRect(360, 170, 104, 70))
self.duixiang9.setObjectName("duixiang9")
self.duixiang10 = QtWidgets.QTextEdit(self.centralwidget)
self.duixiang10.setGeometry(QtCore.QRect(470, 170, 104, 70))
self.duixiang10.setObjectName("duixiang10")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 50, 67, 17))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(180, 50, 67, 17))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(290, 50, 67, 17))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(400, 50, 67, 17))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(510, 50, 67, 17))
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(50, 150, 67, 17))
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(170, 150, 67, 17))
self.label_7.setObjectName("label_7")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(290, 150, 67, 17))
self.label_8.setObjectName("label_8")
self.label_9 = QtWidgets.QLabel(self.centralwidget)
self.label_9.setGeometry(QtCore.QRect(400, 150, 67, 17))
self.label_9.setObjectName("label_9")
self.label_10 = QtWidgets.QLabel(self.centralwidget)
self.label_10.setGeometry(QtCore.QRect(510, 150, 67, 17))
self.label_10.setObjectName("label_10")
self.label_11 = QtWidgets.QLabel(self.centralwidget)
self.label_11.setGeometry(QtCore.QRect(180, 9, 301, 31))
font = QtGui.QFont()
font.setFamily("URW Gothic L")
font.setBold(True)
font.setWeight(75)
self.label_11.setFont(font)
self.label_11.setMouseTracking(False)
self.label_11.setTabletTracking(False)
self.label_11.setAcceptDrops(False)
self.label_11.setObjectName("label_11")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(20, 320, 89, 25))
self.pushButton.setObjectName("pushButton")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(120, 300, 441, 70))
self.textEdit.setObjectName("textEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 588, 28))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(MainWindow.hello)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "1"))
self.label_2.setText(_translate("MainWindow", "2"))
self.label_3.setText(_translate("MainWindow", "3"))
self.label_4.setText(_translate("MainWindow", "4"))
self.label_5.setText(_translate("MainWindow", "5"))
self.label_6.setText(_translate("MainWindow", "6"))
self.label_7.setText(_translate("MainWindow", "7"))
self.label_8.setText(_translate("MainWindow", "8"))
self.label_9.setText(_translate("MainWindow", "9"))
self.label_10.setText(_translate("MainWindow", "10"))
self.label_11.setText(_translate("MainWindow", "请分别输入10个对象进行随机排序"))
self.pushButton.setText(_translate("MainWindow", "paixu"))
注意:需要在开头位置加入import sys
,然后在对定义的setupUi
函数的倒数第二行加入代码,按钮点击:self.pushButton.clicked.connect(MainWindow.hello)
新建文件.py
新建py文件调用上一个导出的文件的类,
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets
import sys
from random import shuffle
from Ui_suijipailie import Ui_MainWindow
class mywindow(QtWidgets.QMainWindow,Ui_MainWindow):
def __init__(self):
super(mywindow,self).__init__()
self.setupUi(self)
#定义槽函数
def hello(self):
list = ['li','wang','wen']
shuffle(list)
str1 = "排序为:" + '. '.join(list)
self.textEdit.setText(str1)
app = QtWidgets.QApplication(sys.argv)
#MainWindow = QMainWindow()
window = mywindow()
window.show()
sys.exit(app.exec_())
此时已经完成了按按钮实现三个对象【li,wang,wen】之间的随机排列并且输出结果。
本文地址:https://blog.csdn.net/weixin_45839124/article/details/107160087
下一篇: 10款效果惊艳的HTML5应用安利