目录
成果图:
1.下载QT designer
下载链接:https://build-system.fman.io/qt-designer-download
2.开始设计
2.1新建窗口,按默认设置,直接点击create即可,因为全都是可以调的,不用担心。
2.2创建组件,直接从左边拖拽组件到窗口界面即可
2.3修改组件属性
1)选中某个组件,右键,点击change styleSheet…
2)像写css一样编写即可,点击apply或者ok就可应用。
常用属性:
背景颜色 | background-color: white; |
边框粗细和颜色 | border: 1px solid #A6A6A6; |
边框圆角 | border-radius:10px; |
字体颜色 | color: red; |
样例一:
background-color: rgb(236,245,255);
color: #409EFF;
border: 1px solid rgb(179,216,255);
border-radius:12px;
样例二:
background-color:rgb(254,240,240);
border: 1px solid rgb(251,196,196);
border-radius:12px;
color: rgb(245,108,108);
样例三:
background-color: white;
color: black;
border: 1px solid #A6A6A6;
border-radius:12px;
样例四:
background-color:#3a9efd;
background-color:#3a9efd;
border:none;
border-radius:80px;
特殊需求:
1.设置透明度:background-color:rgb(250,250,250,160);第四个数设透明度
2.修改字体:选中组件后,在右边的工具栏找到font,点击…即可修改字体
最后保存,生成.ui文件。
3.ui文件转成py文件
把生成的ui文件放在工作文件夹下面,控制台直接运行(在pycharm的terminal中执行,如果直接cmd可能命令不太一样?我也不知道哦):
pyuic5 -o xxx.py xxx.ui
如果没有pyuic5的命令,参考以下配置(其实我有点忘了,反正就一直百度Google总能搞出来了,我也不知道当时怎么配置好的,好像看的就是这篇):
Python+PyQt5+QtDesigner+PyUic+PyRcc(最全安装教程)_sever默默的博客-优快云博客_pyrcc
4.运行
注意,打开生成的py文件,加上主函数而且得import相应的组件:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, QVBoxLayout
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = QMainWindow()
ui = Ui_MainWindow() #Ui_MainWindow 即我们用Qt designer设计生成转化出来的类
ui.setupUi(myWin)
myWin.show()
sys.exit(app.exec_())
5.重写窗口关闭函数
比如在关闭程序之前,我们要释放资源就需要重写关闭函数
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
# 重写close槽函数
def closeEvent(self,event):
print("这里我们重写一下主窗口的close方法")
'''这里可以调用自己的函数,在关闭窗口前会执行'''
super(MyWindow, self).close()
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyWindow() #相应地改成我们重写的类
ui = Ui_MainWindow()
ui.setupUi(myWin)
myWin.show()
sys.exit(app.exec_())
6.多窗口之间互相调用
红色部分为在单页面基础上添加的内容,请注意加粗部分,多一个括号都会报错
myTip = '' #声明一个全局变量 class Ui_MainWindow(object): def setupUi(self, MainWindow): …… #定义一个点击函数,调用myTip的show方法 def pop(self): global myTip myTip.show() def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) MainWindow.setToolTip(_translate("MainWindow", "<html><head/><body><p>hksakdahkjshkdjahskdaks</p></body></html>")) self.pushButton_3.setText(_translate("MainWindow", "生成结果")) self.pushButton_3.clicked.connect(self.pop) self.pushButton.setText(_translate("MainWindow", "选择输入文件")) self.pushButton_2.setText(_translate("MainWindow", "选择输出位置")) self.label.setText(_translate("MainWindow", "<html><head/><body><p>正在处理中,请稍等……</p></body></html>")) #下面是我们新设计的窗口类(同Ui_MainWindow一样的方法创建,只要把类名改掉即可) class Ui_Tip(object): def setupUi(self, Form):…… def retranslateUi(self, Form):…… class MyWindow(QMainWindow): def __init__(self): super(MyWindow, self).__init__() # 重写close槽函数 def close(self): print("这里我们重写一下主窗口的close方法") super(MyWindow, self).close() if __name__ == '__main__': app = QApplication(sys.argv) myWin = MyWindow() ui = Ui_MainWindow() ui.setupUi(myWin) myWin.show() #创建时不调用show函数,点击才调用 myTip = MyWindow() tip = Ui_Tip() tip.setupUi(myTip) sys.exit(app.exec_())
7.给窗口添加图标和标题
import PyQt5.QtGui as qg
class Ui_Tip(object): def setupUi(self, Form):…… def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowIcon(qg.QIcon('error.png')) #图片放在py同级目录下即可 Form.setWindowTitle(_translate("Form", "填写任意名称")) self.label.setText(_translate("Form", "<html><head/><body><p align=\"center\"><span style=\" font-size:10pt; font-weight:600; color:#ff0000;\">您所选择的目录不存在或为空!</span></p></body></html>"))
完整代码,可直接运行:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'hhh.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLineEdit, QVBoxLayout
import PyQt5.QtGui as qg
myTip = ''
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(612, 391)
MainWindow.setStyleSheet("background-color:white;")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(-20, -50, 221, 221))
self.graphicsView.setStyleSheet("background-color:#3a9efd;\n"
"background-color:#3a9efd;\n"
"border:none;\n"
"border-radius:110px;\n"
"\n"
"")
self.graphicsView.setObjectName("graphicsView")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(250, 250, 111, 31))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.pushButton_3.setFont(font)
self.pushButton_3.setStyleSheet("background-color: white;\n"
"color: black;\n"
"border: 1px solid #A6A6A6;\n"
"border-radius:12px;\n"
"")
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(80, 140, 121, 31))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color: white;\n"
"border: 1px solid #A6A6A6;\n"
"border-radius:10px;\n"
"")
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(80, 190, 121, 31))
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(10)
font.setBold(False)
font.setWeight(50)
self.pushButton_2.setFont(font)
self.pushButton_2.setStyleSheet("background-color: white;\n"
"border: 1px solid #A6A6A6;\n"
"border-radius:10px;\n"
"")
self.pushButton_2.setObjectName("pushButton_2")
self.graphicsView_2 = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView_2.setGeometry(QtCore.QRect(-10, -70, 641, 541))
self.graphicsView_2.setStyleSheet("background-color:rgb(250,250,250,160);\n"
"\n"
"border: 1px solid #A6A6A6;\n"
"border-radius:10px;\n"
"")
self.graphicsView_2.setObjectName("graphicsView_2")
self.graphicsView_4 = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView_4.setGeometry(QtCore.QRect(-40, 30, 121, 121))
self.graphicsView_4.setStyleSheet("background-color:#00BFFF;\n"
"border:none;\n"
"border-radius:60px;\n"
"\n"
"")
self.graphicsView_4.setObjectName("graphicsView_4")
self.graphicsView_5 = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView_5.setGeometry(QtCore.QRect(490, 260, 161, 161))
self.graphicsView_5.setStyleSheet("background-color:#3a9efd;\n"
"background-color:#3a9efd;\n"
"border:none;\n"
"border-radius:80px;\n"
"\n"
"")
self.graphicsView_5.setObjectName("graphicsView_5")
self.graphicsView_6 = QtWidgets.QGraphicsView(self.centralwidget)
self.graphicsView_6.setGeometry(QtCore.QRect(150, -10, 81, 81))
self.graphicsView_6.setStyleSheet("background-color:#C6E2FF;\n"
"border:none;\n"
"border-radius:40px;\n"
"\n"
"")
self.graphicsView_6.setObjectName("graphicsView_6")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(210, 140, 321, 31))
self.lineEdit.setStyleSheet("background-color: white;\n"
"border: 1px solid #A6A6A6;\n"
"border-radius:10px;\n"
"")
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(210, 190, 321, 31))
self.lineEdit_2.setStyleSheet("background-color: white;\n"
"border: 1px solid #A6A6A6;\n"
"border-radius:10px;\n"
"")
self.lineEdit_2.setText("")
self.lineEdit_2.setObjectName("lineEdit_2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(90, 300, 401, 21))
font = QtGui.QFont()
font.setFamily("微软雅黑")
self.label.setFont(font)
self.label.setObjectName("label")
self.graphicsView_2.raise_()
self.pushButton_3.raise_()
self.pushButton_2.raise_()
self.graphicsView.raise_()
self.graphicsView_4.raise_()
self.graphicsView_6.raise_()
self.pushButton.raise_()
self.graphicsView_5.raise_()
self.lineEdit.raise_()
self.lineEdit_2.raise_()
self.label.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 612, 26))
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 pop(self):
global myTip
myTip.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
MainWindow.setToolTip(_translate("MainWindow", "<html><head/><body><p>hksakdahkjshkdjahskdaks</p></body></html>"))
self.pushButton_3.setText(_translate("MainWindow", "生成结果"))
self.pushButton_3.clicked.connect(self.pop)
self.pushButton.setText(_translate("MainWindow", "选择输入文件"))
self.pushButton_2.setText(_translate("MainWindow", "选择输出位置"))
self.label.setText(_translate("MainWindow", "<html><head/><body><p>正在处理中,请稍等……</p></body></html>"))
class Ui_Tip(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(283, 104)
Form.setStyleSheet("background-color:white")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(30, 40, 241, 31))
font = QtGui.QFont()
font.setFamily("微软雅黑")
self.label.setFont(font)
self.label.setStyleSheet("QFont font(\"Microsoft YaHei\", 10, 75);")
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowIcon(qg.QIcon('error.png'))
Form.setWindowTitle(_translate("Form", "Error"))
self.label.setText(_translate("Form",
"<html><head/><body><p align=\"center\"><span style=\" font-size:10pt; font-weight:600; color:#ff0000;\">您所选择的目录不存在或为空!</span></p></body></html>"))
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
# 重写close槽函数
def close(self):
print("这里我们重写一下主窗口的close方法")
super(MyWindow, self).close()
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MyWindow()
ui = Ui_MainWindow()
ui.setupUi(myWin)
myWin.show()
myTip = MyWindow()
tip = Ui_Tip()
tip.setupUi(myTip)
sys.exit(app.exec_())
*界面卡顿,出现无响应,变白
请使用多线程编程!
简单方法:
#!/usr/bin/python3 import _thread import time # 为线程定义一个函数 def print_time( threadName, delay): count = 0 while count < 5: time.sleep(delay) count += 1 print ("%s: %s" % ( threadName, time.ctime(time.time()) )) # 创建两个线程 try: _thread.start_new_thread( print_time, ("Thread-1", 2, ) ) _thread.start_new_thread( print_time, ("Thread-2", 4, ) ) except: print ("Error: 无法启动线程") while 1: pass
实用方法(简单方法可能没办法终止线程):
import threading
#自定义一个类,名字自己起,定义下面三个函数(函数名不可改) class DownThread: def __init__(self): self._running = True def terminate(self): self._running = False def run(self): while self._running: '''方法''' #self._running = False(这句话看实际需要) #创建线程方法: c = DownThread() t = threading.Thread(target=c.run, args=()) t.start() #终止线程方法 c.terminate()