PyQt: QtDesigner pyrcc5 练习 3
QtDesigner_练习_3 (pyrcc5)
import sys from PyQt5.Qt import * from untitled_ui import Ui_Form from dialog_ui import Ui_Dialog # 继承Ui_Form类,以便使用Ui_Form中的方法 class Window(QWidget, Ui_Form): def __init__(self): super().__init__() self.setup_ui() self.dialog_setup() def setup_ui(self): # 将self传入Ui_Form类中setupUi实列方法 self.setupUi(self) self.label_3.hide() def dialog_setup(self): # 生成QDialog实例,以便作为Ui_Dialog中控件的父对象 self.dialog_1 = QDialog(self) # 生成Ui_Dialog实例,以便应用setupUi实例方法 self.dialog_1_ui = Ui_Dialog() # 应用setupUi实例方法,将Ui_Dialog中控件的父对象设置为QDialog的实例 self.dialog_1_ui.setupUi(self.dialog_1) def dialog_1_accepted(): self.label_3.setText("点击了OK") self.label_3.show() self.dialog_1.accepted.connect(dialog_1_accepted) def dialog_1_rejected(): self.label_3.setText("点击了Cancel") self.label_3.show() self.dialog_1.rejected.connect(dialog_1_rejected) def slot1(self): if (self.lineEdit.text() == "abc") & (self.lineEdit_2.text() == "123"): # 设置Ui_Dialog中控件label的文本 self.dialog_1_ui.label.setText("登录成功!\n您输入的账户为 用户名:abc 密码:123") # 也可以通过查找QDialog实例的子控件来设置 # self.dialog_1.findChild(QLabel,"label").setText("登录成功!\n您输入的账户为 用户名:abc 密码:123") elif self.lineEdit.text() == "abc": self.dialog_1_ui.label.setText("登录失败!\n密码应为:123") self.lineEdit_2.clear() elif self.lineEdit_2.text() == "123": self.dialog_1_ui.label.setText("登录失败!\n用户名应为:abc") self.lineEdit.clear() else: self.dialog_1_ui.label.setText("登录失败!\n用户名应为:abc 密码应为:123") self.lineEdit.clear() self.lineEdit_2.clear() self.dialog_1.show() if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_()) # untitled.ui ############################################################################# <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>300</width> <height>200</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <layout class="QVBoxLayout" name="verticalLayout" stretch="2,0,1,0,2,0"> <item> <spacer name="verticalSpacer_2"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>40</height> </size> </property> </spacer> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="0,0,0"> <item> <spacer name="horizontalSpacer_2"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> <item> <layout class="QFormLayout" name="formLayout"> <property name="rowWrapPolicy"> <enum>QFormLayout::WrapLongRows</enum> </property> <property name="horizontalSpacing"> <number>6</number> </property> <property name="verticalSpacing"> <number>12</number> </property> <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="text"> <string>用户名(&u):</string> </property> <property name="buddy"> <cstring>lineEdit</cstring> </property> </widget> </item> <item row="0" column="1"> <widget class="QLineEdit" name="lineEdit"> <property name="minimumSize"> <size> <width>100</width> <height>0</height> </size> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_2"> <property name="text"> <string>密码(&p):</string> </property> <property name="buddy"> <cstring>lineEdit_2</cstring> </property> </widget> </item> <item row="1" column="1"> <widget class="QLineEdit" name="lineEdit_2"> <property name="minimumSize"> <size> <width>100</width> <height>0</height> </size> </property> <property name="echoMode"> <enum>QLineEdit::Password</enum> </property> </widget> </item> </layout> </item> <item> <spacer name="horizontalSpacer"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> </layout> </item> <item> <spacer name="verticalSpacer_3"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>40</height> </size> </property> </spacer> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_3"> <item> <widget class="QPushButton" name="pushButton"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="maximumSize"> <size> <width>100</width> <height>16777215</height> </size> </property> <property name="text"> <string>登录</string> </property> </widget> </item> </layout> </item> <item> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>40</height> </size> </property> </spacer> </item> <item> <widget class="QLabel" name="label_3"> <property name="text"> <string>点击了OK</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </widget> <resources/> <connections> <connection> <sender>pushButton</sender> <signal>clicked()</signal> <receiver>Form</receiver> <slot>slot1()</slot> <hints> <hint type="sourcelabel"> <x>151</x> <y>161</y> </hint> <hint type="destinationlabel"> <x>225</x> <y>160</y> </hint> </hints> </connection> </connections> <slots> <slot>slot1()</slot> </slots> </ui> # dialog.ui ############################################################################# <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Dialog</class> <widget class="QDialog" name="Dialog"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>250</width> <height>150</height> </rect> </property> <property name="windowTitle"> <string>Dialog</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QLabel" name="label"> <property name="text"> <string>登录成功!</string> </property> <property name="alignment"> <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set> </property> <property name="margin"> <number>20</number> </property> </widget> </item> <item> <widget class="QDialogButtonBox" name="buttonBox"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="standardButtons"> <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> </property> </widget> </item> </layout> </widget> <resources/> <connections> <connection> <sender>buttonBox</sender> <signal>accepted()</signal> <receiver>Dialog</receiver> <slot>accept()</slot> <hints> <hint type="sourcelabel"> <x>124</x> <y>129</y> </hint> <hint type="destinationlabel"> <x>124</x> <y>74</y> </hint> </hints> </connection> <connection> <sender>buttonBox</sender> <signal>rejected()</signal> <receiver>Dialog</receiver> <slot>reject()</slot> <hints> <hint type="sourcelabel"> <x>124</x> <y>129</y> </hint> <hint type="destinationlabel"> <x>124</x> <y>74</y> </hint> </hints> </connection> </connections> </ui>