编程语言
首页 > 编程语言> > Python 开发GUI之UI界面的三种引入形式

Python 开发GUI之UI界面的三种引入形式

作者:互联网

【纯手工代码】

# -*- coding: utf-8 -*-
# Author:Jack LEE 
# FileName:main
# CreatedDate: 2020/9/17

# 手写代码的基础框架
# 导入需要的库and元件
from PySide2.QtWidgets import QApplication, QMainWindow, QPushButton, QTextEdit

# 初始化函数
def sayHello():
    textEdit.setText("hello")

# 初始化控件
app = QApplication([])
window = QMainWindow()
window.resize(800, 600)
window.move(300, 310)
window.setWindowTitle('MainWindow')
textEdit = QTextEdit("init-information", window)
textEdit.resize(500, 400)
textEdit.move(40, 50)
# textEdit.setText("init-information")
button = QPushButton('pushButton', window)
button.move(600, 300)
button.resize(130, 50)
# button.setText('pushButton')

# 编写信号与槽,以及图像化

button.clicked.connect(sayHello)
window.show()
app.exec_()

 【直接载入UI文件】

# -*- coding: utf-8 -*-
# Author:Jack LEE 
# FileName:UIloader
# CreatedDate: 2020/9/17

import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile


# 定义触发函数
def sayHello():
    window.textEdit.setText("hello world!")
    print("Button click!")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ui_file = QFile("UI/mainWindow.ui")
    ui_file.open(QFile.ReadOnly)
    loader = QUiLoader()
    window = loader.load(ui_file)
    ui_file.close()
    # 在这里加入信号触发、空间位置控制等代码
    window.pushButton.clicked.connect(sayHello)
    # 添加结束
    window.show()
    sys.exit(app.exec_())

【载入UI文件编译后的PY文件】

# -*- coding: utf-8 -*-
# Author:Jack LEE 
# FileName:PYloader
# CreatedDate: 2020/9/17

import sys
# from sys import argv
# 如果使用sys 那将意味着将要打包很多多西,所以可能会出现问题
from mainWindow import Ui_MainWindow
from PySide2.QtWidgets import QMainWindow, QApplication


# 定义信号和槽函数
def signleAndSlot():
    UI.pushButton.clicked.connect(sayHello)


# 定义触发函数
def sayHello():
    UI.textEdit.setText("Hello World!")


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainWindow()
    UI = main.ui
    # 调用信号和槽的函数以及空间管理等函数
    signleAndSlot()
    main.show()
    sys.exit(app.exec_())

 

标签:__,ui,Python,GUI,sys,textEdit,window,UI,import
来源: https://www.cnblogs.com/datawork/p/pyside002.html