欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python qt5 第一个gui编写-10个对象随机排列

程序员文章站 2022-09-14 09:10:07
Python qt5 第一个gui编写-10个对象随机排列笔者运行工作环境:Ubuntu18.04Python3.6.9pyqt5在文本框中输入10个对象,可以对十个对象进行随机排列。全部代码此为全部代码,下边对代码进行解析。# -*- coding: utf-8 -*-# Form implementation generated from reading ui file '/home/li/桌面/python/qt/suijipailie.ui'## Created by: P...

Python qt5 第一个gui编写-10个对象随机排列

笔者运行工作环境:
Ubuntu18.04
Python3.6.9
pyqt5

在文本框中输入10个对象,可以对十个对象进行随机排列。
Python qt5 第一个gui编写-10个对象随机排列

全部代码

此为全部代码,下边对代码进行解析。

# -*- 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
import random

import sys
reload(sys)
sys.setdefaultencoding('utf8')

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"))


class mywindow(QtWidgets.QMainWindow,Ui_MainWindow):

    def __init__(self):
        super(mywindow,self).__init__()
        self.setupUi(self)



    #定义槽函数
    def hello(self):
        list = []
        duixiang1 = self.duixiang1.toPlainText()
        duixiang2 = self.duixiang2.toPlainText()
        duixiang3 = self.duixiang3.toPlainText()
        duixiang4 = self.duixiang4.toPlainText()
        duixiang5 = self.duixiang5.toPlainText()
        duixiang6 = self.duixiang6.toPlainText()
        duixiang7 = self.duixiang7.toPlainText()
        duixiang8 = self.duixiang8.toPlainText()
        duixiang9 = self.duixiang9.toPlainText()
        duixiang10 = self.duixiang10.toPlainText()
        list.append(duixiang1)
        list.append(duixiang2)
        list.append(duixiang3)
        list.append(duixiang4)
        list.append(duixiang5)
        list.append(duixiang6)
        list.append(duixiang7)
        list.append(duixiang8)
        list.append(duixiang9)
        list.append(duixiang10)


        random.shuffle(list)
        str1 = "排列顺序:" + '.    '.join(list)
        self.textEdit.setText(str1)

app = QtWidgets.QApplication(sys.argv)
#MainWindow = QMainWindow()
window = mywindow()
window.show()
sys.exit(app.exec_())

代码解析

1.将ui文件抓变的代码

# -*- 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

reload(sys)
sys.setdefaultencoding('utf8')

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)
        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"))

2.在代码前端导入类

import sys
import random

在代码后边,有个函数shuffle用到了random

2.前端加入代码

import sys
reload(sys)
sys.setdefaultencoding('utf8')

代码如果加入可能会出现报错:
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe6 in position 0: ordinal not in range(128)

4.在生成的代码类的第一个函数的倒数第二行加入:

 self.pushButton.clicked.connect(MainWindow.hello)

加入此代码,形成了一个按钮事件。当鼠标按一下按钮时,就会响应一个事件。

5.在代码后端加入代码

注意:此部分可以重新创建一个文件,然后导入上个文件的类,可以达到同样的效果:

class mywindow(QtWidgets.QMainWindow,Ui_MainWindow):

    def __init__(self):
        super(mywindow,self).__init__()
        self.setupUi(self)



    #定义槽函数
    def hello(self):
        list = []
        duixiang1 = self.duixiang1.toPlainText()
        duixiang2 = self.duixiang2.toPlainText()
        duixiang3 = self.duixiang3.toPlainText()
        duixiang4 = self.duixiang4.toPlainText()
        duixiang5 = self.duixiang5.toPlainText()
        duixiang6 = self.duixiang6.toPlainText()
        duixiang7 = self.duixiang7.toPlainText()
        duixiang8 = self.duixiang8.toPlainText()
        duixiang9 = self.duixiang9.toPlainText()
        duixiang10 = self.duixiang10.toPlainText()
        list.append(duixiang1)
        list.append(duixiang2)
        list.append(duixiang3)
        list.append(duixiang4)
        list.append(duixiang5)
        list.append(duixiang6)
        list.append(duixiang7)
        list.append(duixiang8)
        list.append(duixiang9)
        list.append(duixiang10)


        random.shuffle(list)
        str1 = "排列顺序:" + '.    '.join(list)
        self.textEdit.setText(str1)

app = QtWidgets.QApplication(sys.argv)
#MainWindow = QMainWindow()
window = mywindow()
window.show()
sys.exit(app.exec_())

这部分的代码,前半部分和后半部分都是固定格式,后半部分是将创建的类实例化。主要关注的代码为中间部分。
首先创建了一个空的列表,list = [],然后读取文本框读取的对象,然后在利用append函数,将读取的对象添加到空的列表中。之后利用random函数,将列表中的对象进行随机排列。
因为self.textEdit.setText函数是将字符串输入显示到文本框,所以利用join()将随机排列的列表转换为字符串。
此为代码的全部过程和解析。

参考链接:

PyQt5之多行文本框(QtextEdit)控件中的常用方法和信号
注意次代码中常用的方法,在读取文本框的字符串时,一直出问题,找了半天资料也没有解决,后来看到次链接解决了问题。

PyQt5笔记(04) – 文本框的使用
此代码可以参考一下,对我写代码有一定的启发,直接运行需要在代码前端加一句:# -*- coding: utf-8 -*-
若报错:super().init()
TypeError: super() takes at least 1 argument (0 given),可能为运行Python版本不对,在终端中用Python3加文件名运行。
运行结果:在文本框中输入字符串,可以再给显示出来。
Python qt5 第一个gui编写-10个对象随机排列
使用PyQt来编写第一个Python GUI程序
此链接用qt4写的,但是在写开始部分的时候具有较大的参考价值。

【QTdesigner】课时36.使用QTextEdit控件输入多行文本【pyqt5+QTdesigner模式】

PyQt5,弹窗,自定义输入对话框

pyqt5实现注册界面并获得文本框内容

pyqt5读取文本框内容,输出到日志框(QTextBrowser)

本文地址:https://blog.csdn.net/weixin_45839124/article/details/107167607