Skip to main content

只要一个ui文件,自动加载对应的逻辑类.

Project description

qt-UILoader1.0

调用该工具的方法

只需要给一个.ui界面文件,会自动加载一个逻辑类与之匹配。

你可以很方便的在逻辑类里写你的功能

要用到什么组件只需要在逻辑类里声明变量。记得变量名必须要和ui里的objectName里的一样

img.png

案例:

注意1:

mainWindow.ui 和 mainWindow_logic.py必须在同一目录

注意2:

逻辑类的名字是ui文件名下划线加logic。 mainWindow_logic.py

注意3:

库用的是PyQt6

1.mainWindow.ui 这是界面文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
 <x>0</x>
 <y>0</y>
 <width>300</width>
 <height>200</height>
</rect>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout">
 <item>
  <widget class="QLineEdit" name="lineEdit"/>
 </item>
 <item>
  <widget class="QPushButton" name="pushButton">
   <property name="text">
    <string>Click Me</string>
   </property>
  </widget>
 </item>
</layout>
</widget>
</widget>
</ui>

2.在旁边建立一个mainWindow_logic.py文档。

from PyQt6.QtWidgets import QPushButton, QLineEdit

class MainWindowLogic:
 pushButton: QPushButton  # 必须与UI中的objectName一致
 lineEdit: QLineEdit

 def setup_connections(self):
     """显式信号连接"""
     self.pushButton.clicked.connect(self.on_button_clicked)
     print("显式连接成功")

 def on_button_clicked(self):
     """按钮点击事件处理"""
     text = self.lineEdit.text() or "World"
     print(f"[显式] Hello {text}!")

 def pushButton_clicked(self):
     """自动连接事件(命名规范:组件名_信号名)"""
     print("[自动] 按钮点击事件通过命名规范连接成功")

3 调用

if __name__ == "__main__":
    from PyQt6.QtWidgets import QApplication

    # 确保工作目录正确(如果从父目录运行)
    os.chdir(os.path.dirname(__file__))

    app = QApplication([])

    # 使用相对路径示例
    loader = UILoader("mainWindow.ui")  # UI文件在子目录test1中
    window, logic = loader.load()
    window.show()

    app.exec()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qt_uiloader-0.0.1.tar.gz (32.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qt_uiloader-0.0.1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file qt_uiloader-0.0.1.tar.gz.

File metadata

  • Download URL: qt_uiloader-0.0.1.tar.gz
  • Upload date:
  • Size: 32.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for qt_uiloader-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3ffad2aa72738ead235e5d4c169d296acd03b94b4ea908184c6d3bd3620b8fcb
MD5 fbf8bd1b0dc990b81d51260e9f483d26
BLAKE2b-256 981ae02eac3b8ce9f5024dc8ce0a39ef081045711030189dc117f468bf15e626

See more details on using hashes here.

File details

Details for the file qt_uiloader-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: qt_uiloader-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.13

File hashes

Hashes for qt_uiloader-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6605996639f734de76a102f133bf9ca88061a616a8b8f0143a136f4bdb075eeb
MD5 3372a3dc339ca4b72536e37ebac69e64
BLAKE2b-256 dd26e0fffb1bdbe3d555f30c58a473c72b760d9a29ff9fdb768bcc0366d137c2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page