[补充]
错误代码:
import sysimport os
from PySide6.QtCore import QFile, Slot # Qt文件处理器
from PySide6.QtUiTools import QUiLoader # UI文件导入器
from PySide6.QtWidgets import * # 导入Qt组件
@Slot()
def clicked():
name = window.name.toPlainText()
window.close()
ui_file = QFile("name.ui") # 设置UI文件为QFile对象
ui_file.open(QFile.ReadWrite) # 读写模式打开UI文件
ui_file.close() # 关闭文件
loader = QUiLoader() # 生成UI加载器
window2 = loader.load(ui_file) # 加载UI文件
window2.Text.text = "你好," + name
ui_file.close() # 关闭文件 window2.show()
app = QApplication(sys.argv) # 主APP
ui_file = QFile("mainWindow.ui") # 设置UI文件为QFile对象
ui_file.open(QFile.ReadOnly) # 只读模式打开
UI文件ui_file.close() # 关闭文件loader = QUiLoader() # 生成UI加载器
window = loader.load(ui_file) # 加载UI文件window.confirm.clicked.connect(clicked)
ui_file.close() # 关闭文件window.show()
sys.exit(app.exec()) # 退出
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>557</width> <height>364</height> </rect> </property> <property name="windowTitle"> <string>测试</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>80</x> <y>20</y> <width>201</width> <height>51</height> </rect> </property> <property name="text"> <string><font size = 30><b>请输入你的姓名</b></font></string> </property> </widget> <widget class="QCommandLinkButton" name="confirm"> <property name="geometry"> <rect> <x>20</x> <y>180</y> <width>511</width> <height>61</height> </rect> </property> <property name="text"> <string>确定</string> </property> <property name="checkable"> <bool>false</bool> </property> <property name="description"> <string>下一步</string> </property> </widget> <widget class="QCommandLinkButton" name="unusable"> <property name="enabled"> <bool>false</bool> </property> <property name="geometry"> <rect> <x>20</x> <y>250</y> <width>511</width> <height>61</height> </rect> </property> <property name="text"> <string>退出</string> </property> <property name="checkable"> <bool>false</bool> </property> <property name="description"> <string>取消并退出</string> </property> </widget> <widget class="QTextEdit" name="name"> <property name="geometry"> <rect> <x>50</x> <y>90</y> <width>451</width> <height>71</height> </rect> </property> <property name="placeholderText"> <string>在此处输入您的姓名...</string> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>557</width> <height>22</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/></ui>
name.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>348</width> <height>126</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QLabel" name="Text"> <property name="geometry"> <rect> <x>20</x> <y>20</y> <width>321</width> <height>71</height> </rect> </property> <property name="text"> <string><font face = '经典行书简' color = 'red' size = 40><b>你好,</b></font></string> </property> <property name="textFormat"> <enum>Qt::AutoText</enum> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>348</width> <height>22</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/></ui>
错误代码:
import sysimport os
from PySide6.QtCore import QFile, Slot # Qt文件处理器
from PySide6.QtUiTools import QUiLoader # UI文件导入器
from PySide6.QtWidgets import * # 导入Qt组件
@Slot()
def clicked():
name = window.name.toPlainText()
window.close()
ui_file = QFile("name.ui") # 设置UI文件为QFile对象
ui_file.open(QFile.ReadWrite) # 读写模式打开UI文件
ui_file.close() # 关闭文件
loader = QUiLoader() # 生成UI加载器
window2 = loader.load(ui_file) # 加载UI文件
window2.Text.text = "你好," + name
ui_file.close() # 关闭文件 window2.show()
app = QApplication(sys.argv) # 主APP
ui_file = QFile("mainWindow.ui") # 设置UI文件为QFile对象
ui_file.open(QFile.ReadOnly) # 只读模式打开
UI文件ui_file.close() # 关闭文件loader = QUiLoader() # 生成UI加载器
window = loader.load(ui_file) # 加载UI文件window.confirm.clicked.connect(clicked)
ui_file.close() # 关闭文件window.show()
sys.exit(app.exec()) # 退出
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>557</width> <height>364</height> </rect> </property> <property name="windowTitle"> <string>测试</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>80</x> <y>20</y> <width>201</width> <height>51</height> </rect> </property> <property name="text"> <string><font size = 30><b>请输入你的姓名</b></font></string> </property> </widget> <widget class="QCommandLinkButton" name="confirm"> <property name="geometry"> <rect> <x>20</x> <y>180</y> <width>511</width> <height>61</height> </rect> </property> <property name="text"> <string>确定</string> </property> <property name="checkable"> <bool>false</bool> </property> <property name="description"> <string>下一步</string> </property> </widget> <widget class="QCommandLinkButton" name="unusable"> <property name="enabled"> <bool>false</bool> </property> <property name="geometry"> <rect> <x>20</x> <y>250</y> <width>511</width> <height>61</height> </rect> </property> <property name="text"> <string>退出</string> </property> <property name="checkable"> <bool>false</bool> </property> <property name="description"> <string>取消并退出</string> </property> </widget> <widget class="QTextEdit" name="name"> <property name="geometry"> <rect> <x>50</x> <y>90</y> <width>451</width> <height>71</height> </rect> </property> <property name="placeholderText"> <string>在此处输入您的姓名...</string> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>557</width> <height>22</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/></ui>
name.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>348</width> <height>126</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QLabel" name="Text"> <property name="geometry"> <rect> <x>20</x> <y>20</y> <width>321</width> <height>71</height> </rect> </property> <property name="text"> <string><font face = '经典行书简' color = 'red' size = 40><b>你好,</b></font></string> </property> <property name="textFormat"> <enum>Qt::AutoText</enum> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>348</width> <height>22</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/></ui>