溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

利用pyuic5將ui文件轉(zhuǎn)換為py文件的方法

發(fā)布時間:2020-09-05 08:48:18 來源:腳本之家 閱讀:319 作者:yanwucao 欄目:開發(fā)技術(shù)

操作系統(tǒng)上正確配置python環(huán)境之后,pyuic5也是一個可以識別的命令行指令

到.ui文件的目錄下,直接cmd進入,輸入pyuic5 -o 轉(zhuǎn)換的py文件 待轉(zhuǎn)換的ui文件

利用pyuic5將ui文件轉(zhuǎn)換為py文件的方法

此時,需要對login.py添加一點代碼使得設(shè)計好的UI能夠出現(xiàn)在我們面前

import sys
 
 
if __name__ == "__main__":
  app = QtWidgets.QApplication(sys.argv) # 創(chuàng)建一個QApplication,也就是你要開發(fā)的軟件app
  MainWindow = QtWidgets.QMainWindow()  # 創(chuàng)建一個QMainWindow,用來裝載你需要的各種組件、控件
  ui = Ui_Form()             # ui是你創(chuàng)建的ui類的實例化對象
  ui.setupUi(MainWindow)         # 執(zhí)行類中的setupUi方法,方法的參數(shù)是第二步中創(chuàng)建的QMainWindow
  MainWindow.show()            # 執(zhí)行QMainWindow的show()方法,顯示這個QMainWindow
  sys.exit(app.exec_())          # 使用exit()或者點擊關(guān)閉按鈕退出QApplication

完整代碼段如下:

# -*- coding: utf-8 -*-
 
# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
 
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
 
 
class Ui_Form(object):
  def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(400, 300)
    self.pushButton = QtWidgets.QPushButton(Form)
    self.pushButton.setGeometry(QtCore.QRect(70, 220, 75, 23))
    self.pushButton.setObjectName("pushButton")
    self.pushButton_2 = QtWidgets.QPushButton(Form)
    self.pushButton_2.setGeometry(QtCore.QRect(220, 220, 75, 23))
    self.pushButton_2.setObjectName("pushButton_2")
    self.checkBox = QtWidgets.QCheckBox(Form)
    self.checkBox.setGeometry(QtCore.QRect(70, 180, 141, 16))
    self.checkBox.setObjectName("checkBox")
    self.lineEdit = QtWidgets.QLineEdit(Form)
    self.lineEdit.setGeometry(QtCore.QRect(130, 56, 181, 20))
    self.lineEdit.setObjectName("lineEdit")
    self.lineEdit_2 = QtWidgets.QLineEdit(Form)
    self.lineEdit_2.setGeometry(QtCore.QRect(130, 110, 181, 20))
    self.lineEdit_2.setObjectName("lineEdit_2")
    self.label = QtWidgets.QLabel(Form)
    self.label.setGeometry(QtCore.QRect(70, 60, 54, 12))
    self.label.setObjectName("label")
    self.label_2 = QtWidgets.QLabel(Form)
    self.label_2.setGeometry(QtCore.QRect(70, 110, 54, 12))
    self.label_2.setObjectName("label_2")
 
    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)
 
  def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Form"))
    self.pushButton.setText(_translate("Form", "取消"))
    self.pushButton_2.setText(_translate("Form", "確定"))
    self.checkBox.setText(_translate("Form", "記住用戶名和密碼"))
    self.label.setText(_translate("Form", "用戶名:"))
    self.label_2.setText(_translate("Form", "密碼:"))
 
 
if __name__ == "__main__":
  app = QtWidgets.QApplication(sys.argv) # 創(chuàng)建一個QApplication,也就是你要開發(fā)的軟件app
  MainWindow = QtWidgets.QMainWindow()  # 創(chuàng)建一個QMainWindow,用來裝載你需要的各種組件、控件
  ui = Ui_Form()             # ui是你創(chuàng)建的ui類的實例化對象
  ui.setupUi(MainWindow)         # 執(zhí)行類中的setupUi方法,方法的參數(shù)是第二步中創(chuàng)建的QMainWindow
  MainWindow.show()            # 執(zhí)行QMainWindow的show()方法,顯示這個QMainWindow
  sys.exit(app.exec_())          # 使用exit()或者點擊關(guān)閉按鈕退出QApplication
 

結(jié)果顯示如下:

利用pyuic5將ui文件轉(zhuǎn)換為py文件的方法

以上這篇利用pyuic5將ui文件轉(zhuǎn)換為py文件的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI