【PyQt5篇】使用QtDesigner添加控件和槽

文章目录

🍔使用QtDesigner进行设计

我们首先使用QtDesigner设计界面

得到代码login.ui

python 复制代码
<?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>478</width>
    <height>220</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>30</y>
     <width>72</width>
     <height>15</height>
    </rect>
   </property>
   <property name="text">
    <string>用户名:</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>21</x>
     <y>74</y>
     <width>71</width>
     <height>21</height>
    </rect>
   </property>
   <property name="text">
    <string>密码:</string>
   </property>
  </widget>
  <widget class="QTextBrowser" name="textBrowser">
   <property name="geometry">
    <rect>
     <x>215</x>
     <y>20</y>
     <width>201</width>
     <height>91</height>
    </rect>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>150</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>登录</string>
   </property>
  </widget>
  <widget class="QPushButton" name="pushButton_2">
   <property name="geometry">
    <rect>
     <x>140</x>
     <y>150</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>忘记密码</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>30</y>
     <width>113</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QLineEdit" name="lineEdit_2">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>70</y>
     <width>113</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

🛸在代码中添加信号和槽

我们看下面的代码

python 复制代码
import sys

from PyQt5.QtWidgets import *
from PyQt5 import uic

class MyWindow(QWidget):

    def __init__(self):
    
        super().__init__()  #这段代码不能少
        
        self.init_ui()

    def init_ui(self):
		
		# 引入ui文件
        self.ui=uic.loadUi("./login.ui",self)

        self.user_name=self.ui.lineEdit
        self.password=self.ui.lineEdit_2
        self.login_btn=self.ui.pushButton
        self.forget_btn=self.ui.pushButton_2
        self.text_Browser=self.ui.textBrowser  #文本显示区域

        # 绑定信号和槽函数
        self.login_btn.clicked.connect(self.login)
        self.forget_btn.clicked.connect(self.forget)


    def login(self):
        # 实现登录的逻辑
        user_name=self.user_name.text()
        password=self.password.text()
        if user_name == "admin" and password == "123456":
            print("登录成功")
            self.text_Browser.setText("欢迎%s"% user_name)
            self.text_Browser.repaint()
        else:
            print("用户名或密码错误")
            self.text_Browser.setText("用户名或密码错误")
            self.text_Browser.repaint()


    def forget(self):
        print("忘记密码")

if __name__=='__main__':
    app=QApplication(sys.argv)

    window=MyWindow()

    window.show()

    app.exec_()

运行结果

相关推荐
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔3 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号3 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_3 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty3 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再3 天前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame