python如何调用ui文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python如何调用ui文件相关的知识,希望对你有一定的参考价值。
ui文件是Qt生成xml格式的文件,python要使用就得用pyside 或pyqt这其中一个库才能将ui文件转化为py文件,安装pyside库后,在命令行打这一句 : pyside-uic XXX.ui -o XXX_ui.py 参考技术A python如何调用ui文件的代码如下:#-*- coding: gbk -*-
#加载有需要的Qt库
from PyQt4 import QtCore, QtGui
#从ui_test.py文件中读取Ui_Dialog类,Ui_Dialog类由pyuic4生成
from ui_test import Ui_Dialog
#继承的类要和生成的ui对相
class Ui(QtGui.QDialog):
def __init__(self, parent=None):
super(Ui, self).__init__(parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Ui()
window.show()
sys.exit(app.exec_())
如何更改在pyqt5 python3中单击按钮时显示的.ui文件
【中文标题】如何更改在pyqt5 python3中单击按钮时显示的.ui文件【英文标题】:How to change .ui file that is displayed on button click in pyqt5 python3 【发布时间】:2020-04-05 10:48:07 【问题描述】:我想更改按钮单击时的显示。 我当前的代码:
import sys
from PyQt5 import QtWidgets, uic
Ui_bankApp, _ = uic.loadUiType("bankApp.ui")
Ui_Borrow, _ = uic.loadUiType("Borrow.ui")
Ui_Login, _ = uic.loadUiType("login.ui")
Ui_Pay, _ = uic.loadUiType("Pay.ui")
class Borrow(object):
def setupUI(self, Ui):
uic.loadUi('Borrow.ui', self)
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
self.bankApp()
def bankApp(self):
print("bankApp")
self.ui = Ui_bankApp()
self.ui.setupUi(self)
self.hide()
self.show()
self.button = self.findChild(QtWidgets.QPushButton, 'pushButton_2')
self.button.clicked.connect(self.Borrow)
def Borrow(self):
print("Borrow")
self.ui1 = Ui_Borrow()
self.ui1.setupUi(self)
self.button1 = self.findChild(QtWidgets.QPushButton, 'pushButton_2')
self.button1.clicked.connect(self.bankApp)
def Login(selft):
self.ui = Ui_Login()
self.ui.setupUi(self)
def Pay():
self.ui = Ui_Pay()
self.ui.setupUi(self)
app = QtWidgets.QApplication(sys.argv)
window = Ui()
window.show()
app.exec_()
我的两个 ui 文件如下所示: 银行应用程序.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>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>80</x>
<y>190</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Pay</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>610</x>
<y>180</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Borrow</string>
</property>
</widget>
<widget class="QLCDNumber" name="lcdNumber">
<property name="geometry">
<rect>
<x>210</x>
<y>30</y>
<width>321</width>
<height>91</height>
</rect>
</property>
<property name="intValue" stdset="0">
<number>1000</number>
</property>
</widget>
<widget class="QLCDNumber" name="lcdNumber_2">
<property name="geometry">
<rect>
<x>600</x>
<y>270</y>
<width>131</width>
<height>51</height>
</rect>
</property>
</widget>
<widget class="QLCDNumber" name="lcdNumber_3">
<property name="geometry">
<rect>
<x>600</x>
<y>370</y>
<width>131</width>
<height>51</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>600</x>
<y>230</y>
<width>91</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Spent</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>600</x>
<y>340</y>
<width>47</width>
<height>13</height>
</rect>
</property>
<property name="text">
<string>Earned</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>40</x>
<y>250</y>
<width>47</width>
<height>13</height>
</rect>
</property>
<property name="text">
<string>Debt</string>
</property>
</widget>
<widget class="QLCDNumber" name="lcdNumber_4">
<property name="geometry">
<rect>
<x>40</x>
<y>270</y>
<width>141</width>
<height>51</height>
</rect>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</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>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>Borrow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>20</x>
<y>20</y>
<width>51</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>Back</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>220</x>
<y>290</y>
<width>281</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>Borrow</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>220</x>
<y>200</y>
<width>281</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>Amount</string>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
所以当我单击bankApp 中的借用按钮时,我想切换到借用页面,然后当我按下返回按钮时,我想切换到bankApp 页面。 但是当我点击借阅页面上的后退按钮时,它不会返回到 bankApp 页面
【问题讨论】:
【参考方案1】:您可以考虑使用QStackedWidget
在布局之间切换,而不是即时更改用户界面。例如,
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.stacked_widget = QtWidgets.QStackedWidget()
self.bankApp()
self.Borrow()
self.setCentralWidget(self.stacked_widget)
self.resize(800,600)
def bankApp(self):
self.bank_widget = QtWidgets.QMainWindow()
Ui_bankApp().setupUi(self.bank_widget)
self.stacked_widget.addWidget(self.bank_widget)
self.button = self.bank_widget.findChild(QtWidgets.QPushButton, 'pushButton_2')
self.button.clicked.connect(lambda : self.goto_page(self.borrow_widget))
def Borrow(self):
self.borrow_widget = QtWidgets.QMainWindow()
Ui_Borrow().setupUi(self.borrow_widget)
self.stacked_widget.addWidget(self.borrow_widget)
self.button1 = self.borrow_widget.findChild(QtWidgets.QPushButton, 'pushButton_2')
self.button1.clicked.connect(lambda : self.goto_page(self.bank_widget))
def goto_page(self, widget):
index = self.stacked_widget.indexOf(widget)
print(index, widget)
if index >= 0:
self.stacked_widget.setCurrentIndex(index)
【讨论】:
如何将变量添加到 self.goto_page(self.borrow_widget)) 喜欢 self.borrow_widget(var,var2)以上是关于python如何调用ui文件的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 PyQt5 和 python 3.6 将 .ui 文件转换为 .py 文件
如何将变量从 CLI 程序传递到 Python 视图以更新 Django UI?
Python Qt GUI设计:Python调用UI文件的两种方法(基础篇—3)