当前位置: 首页 > article >正文

python:PyQt5 简单示例

cd \Python37\Script

pip install pyqt5; 

pip install pyqt5-tools; 

dir 可见 qt5-tools.exe , pyuic5.exe , pyrcc5.exe 等

运行 qt5-tools.exe designer 设计应用界面,生成文件 ui_main.ui ,

运行 pyuic5.exe ui_main.ui -o ui_main.py

生成 ui_main.py 再继续编写事件处理子程式

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui_main.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *

class Ui_Dialog(QWidget):

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(640, 480)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(10, 10, 72, 15))
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(100, 10, 521, 21))
        self.lineEdit.setObjectName("lineEdit")
        self.button1 = QtWidgets.QPushButton(Dialog)
        self.button1.setGeometry(QtCore.QRect(40, 40, 93, 28))
        self.button1.setObjectName("button1")
        self.button2 = QtWidgets.QPushButton(Dialog)
        self.button2.setGeometry(QtCore.QRect(160, 40, 93, 28))
        self.button2.setObjectName("button2")
        self.browser = QtWidgets.QTextBrowser(Dialog)
        self.browser.setGeometry(QtCore.QRect(10, 80, 621, 381))
        self.browser.setObjectName("browser")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        #Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "文件名:"))
        self.button1.setText(_translate("Dialog", "打开文件"))
        self.button1.clicked.connect(self.button1_click)
        self.button2.setText(_translate("Dialog", "选择目录"))
        self.button2.clicked.connect(self.button2_click)

    def button1_click(self):
        file1 = QFileDialog.getOpenFileName(self, "选取文件", os.getcwd(), "")
        self.lineEdit.setText(file1[0])

    def button2_click(self):
        path1 = QFileDialog.getExistingDirectory(self, "选文件夹", os.getcwd())
        self.lineEdit.setText(path1)

pyqt5_test.py

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import ui_main # pyuic5.exe ui_main.ui -o ui_main.py

if __name__ == '__main__':
	app = QApplication(sys.argv)
	mainw = QMainWindow()
	mainw.setWindowTitle('简单示例')
	ui = ui_main.Ui_Dialog()
	ui.setupUi(mainw)
	mainw.show()
	sys.exit(app.exec_())

运行 python pyqt5_test.py 


http://www.kler.cn/a/14212.html

相关文章:

  • 应届生的天坑,悔不该进那外包啊.....
  • linux 命令之 tar -czvf和 tar -xzvf
  • nodejs中使用json
  • 【Linux命令行与shell脚本编程】 一,Shell简介
  • 图像处理学习——基于霍夫变换(Matlab代码实现)
  • 数据结构对齐
  • Trie|并查集|堆|
  • maven作用讲解---以及怎么配置阿里的maven镜像
  • 2023浙江省赛“信息安全管理与评估“--Web渗透测试(高职组)
  • Python每日一练(20230420)
  • 【MySQL笔记】MySQL数据库分库分表解读
  • java版UWB人员定位系统源码,提供位置实时显示、历史轨迹回放、电子围栏、行为分析、智能巡检等功能
  • css-设置单行文本溢出省略号,使用overflow:hidden属性之后的出现的问题几解决办法。
  • Hilt在Android中的使用
  • 深入理解 JavaScript 中的原型和原型链
  • 数据库和缓存一致性问题
  • 盘点C# 9.0中好用的特性
  • Win7系统USB串口安装驱动出现“系统找不到指定的文件“解决方案
  • 计算机组成原理 指令系统(1)
  • itop-3568开发板驱动学习笔记(22)设备树(一)设备树基础