Pyqt5使用笔记
本文最后更新于:2021年1月8日 晚上
- 将
.ui
文件转换为.py
文件
python3 -m PyQt5.uic.pyuic untitled.ui -o untitled.py
界面和逻辑分离
创建逻辑文件
.py
,继承界面文件的主窗口类,这样便可实现界面和逻辑的分离。当需要更新界面时,只需要对界面文件.ui
进行更新,再转换为对应的.py
文件即可示例程序1
#导入相关子模块
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
# Subclass QMainWindow to customise your application's main window
# 通过子类化QMainwindow来自定义应用程序主窗口
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("My Awesome App") #设置窗口标题
label = QLabel("THIS IS AWESOME!!!")
# The `Qt` namespace has a lot of attributes to customise
# widgets. See: http://doc.qt.io/qt-5/qt.html
label.setAlignment(Qt.AlignCenter)
# Set the central widget of the Window. Widget will expand
# to take up all the space in the window by default.
self.setCentralWidget(label)
#每个应用程序需要且仅需要一个QApplication实例,传入sys.argv以允许应用程序访问命令行参数
app = QApplication(sys.argv)
window = QMainWindow() #每个应用程序至少有一个QMainWindow
window.show() # window默认处于关闭状态
# 开始事件循环(event loop)
app.exec_()
- 示例程序2
#!/usr/bin/python3
import sys
import os
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class picture(QWidget):
def __init__(self):
super(picture, self).__init__()
self.resize(860, 360)
self.setWindowTitle("WSCL")
self.label = QLabel(self)
self.label.setText("pre_image")
self.label.move(130, 30)
self.label2 = QLabel(self)
#self.label2.setText("pre_image")
self.label2.setFixedSize(300, 200)
self.label2.move(30, 60)
self.label2.setStyleSheet("QLabel{background:white;}"
)
self.label3 = QLabel(self)
self.label3.setText("results")
self.label3.move(520, 30)
self.label4 = QLabel(self)
#self.label4.setText("results")
self.label4.setFixedSize(300, 200)
self.label4.move(430, 60)
self.label4.setStyleSheet("QLabel{background:white;}")
btn = QPushButton(self)
btn.setText("open")
btn.move(130, 300)
btn.clicked.connect(self.openimage)
btn = QPushButton(self)
btn.setText("start")
btn.move(320, 300)
btn.clicked.connect(self.dectimage)
btn = QPushButton(self)
btn.setText("close")
btn.move(520, 300)
btn.clicked.connect(self.close)
def openimage(self):
global imgName
imgName, imgType = QFileDialog.getOpenFileName(self, "open", "", "*.jpg;;*.png;;All Files(*)")
jpg = QtGui.QPixmap(imgName).scaled(self.label2.width(), self.label2.height())
self.label2.setPixmap(jpg)
#self.statusbar.showMessage(imgName)
def dectimage(self):
print(imgName)
#os.system(echo imgName)
os.system('pwd')
os.environ['imgName'] = str(imgName)
os.system('echo $imgName')
os.system('./experiments/scripts/test.sh 0 pascal_voc vgg16 voc12_wsdnn_pre $imgName')
#./experiments/scripts/test.sh 0 pascal_voc vgg16 voc12_wsdnn_pre /home/amax/Pictures/4.jpg
rawimgName=imgName.split("/")[4].split(".")[0]
newimgpath='/home/amax/WSCL_vgg16_voc12_wsdnn_pre/cache/voc_2007_test/default/vgg16_faster_rcnn_iter_200000/faster/detections/'+ rawimgName +'.jpg'
jpg2 = QtGui.QPixmap(newimgpath).scaled(self.label4.width(), self.label4.height())
self.label4.setPixmap(jpg2)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
my = picture()
my.show()
sys.exit(app.exec_())
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!