只要一个ui文件,自动加载对应的逻辑类.
Project description
qt-UILoader
调用该工具的方法
只需要给一个.ui界面文件,会自动加载一个逻辑类与之匹配。
你可以很方便的在逻辑类里写你的功能
要用到什么组件只需要在逻辑类里声明变量。记得变量名必须要和ui里的objectName里的一样
案例:
注意1:
mainWindow.ui 和 mainWindow_logic.py必须在同一目录
注意2:
逻辑类的名字是ui文件名下划线加logic。 mainWindow_logic.py
注意3:
库用的是PyQt6
更新日志2025年3月11日
改进了事件的自动连接和显示连接。自动连接的命名规则是on_pushButton_clicked。由三部分构成on、_pushButton、_clicked。分别是:on开头,_pushButton组件的objectName,_clicked事件名称
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qt_uiloader-1.0.3.tar.gz.
File metadata
- Download URL: qt_uiloader-1.0.3.tar.gz
- Upload date:
- Size: 33.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
31428f5c3a788b10995d40fa7da63584ac9651ee9341c14ff4d46c8fadcd78b1
|
|
| MD5 |
742e50e7d8ba688aa9815e0a296c8418
|
|
| BLAKE2b-256 |
ba4fb73a9395fd0bb6fb97eb43e8957f72f386e4870a802ebddfe5a5aa860fc9
|
File details
Details for the file qt_uiloader-1.0.3-py3-none-any.whl.
File metadata
- Download URL: qt_uiloader-1.0.3-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f403dd8335da16011d39824bf0552ad9fb5c9f8e1fbec1b5ff7cbcb4b5e42c7
|
|
| MD5 |
eb6b4518f4cd00b089bfdee515b0a3b4
|
|
| BLAKE2b-256 |
659f0b125d477e06a04822203c9d8590d03332c8001b0ef12fd50771b379732d
|