PyQt5 加载UI文件
PyQt5直接加载UI文件,而不是将其转化为.py文件。
PySide2直接加载UI文件
通过Qt Creator新建Qt for Python项目
自动生成直接加载UI文件代码:
1 |
|
PyQt5直接加载UI文件
仿造上述代码
```python
This Python file uses the following encoding: utf-8
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.uic import loadUi
class UITest(QWidget):
def init(self, parent=None):
super().init()
loadUi(‘form.ui’, self)
self.setFixedSize(600, 400)
self.pushButton.clicked.connect(self.onButtonClicked)#可直接self.pushButton,该QPushButton变量在UI中定义
def onButtonClicked(self):
x = random.randint(self.pushButton.width(), self.width() - self.pushButton.width())
y = random.randint(self.pushButton.height(), self.height() - self.pushButton.height())
self.pushButton.move(x, y)
if name == “main“:
app = QApplication([])
widget = UITest()
widget.show()
sys.exit(app.exec_())
项目
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!