PySide6起步

1. PySide6安装

前提是先安装好python,然后,执行下面命令进行安装

pip install PySide6 -i https://pypi.tuna.tsinghua.edu.cn/simple

安装后,通过下面命令可以查看安装的位置

pip show pyside6

2. QtDesigner简单介绍

通过安装后的PySide6的安装目录,找到designer.exe,运行,就能到QtDesigner

左边是各种控件,中间是UI的设计区域,右边是配置属性

PySide6起步_第1张图片

 下面一步步创建一个简单登录的UI界面

1. 先创建一个Widget,右边把objectName修改为Login_Form,这个会在python脚本作为变量名称调用;同时,把windowTitle设置为Login;接下来保存文件为login.ui到电脑某个位置

PySide6起步_第2张图片

2. 添加两个Label,分别为Name和Password,同样也会修改对应的objectName,以及再添加两个LineEdit控件,作为输入;添加一个pushButton点击登录,一个ToolButton作为Password的查看

 PySide6起步_第3张图片

3. 转化UI为python脚本

3. 保存login.ui, 使用下面命令,将ui转为python脚本

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QLineEdit, QPushButton,
    QSizePolicy, QToolButton, QWidget)

class Ui_Login_Form(object):
    def setupUi(self, Login_Form):
        if not Login_Form.objectName():
            Login_Form.setObjectName(u"Login_Form")
        Login_Form.setEnabled(True)
        Login_Form.resize(405, 144)

4. 运行第一个UI

4. 编写mian.py,调用这个ui,运行程序

绑定两个按键的事件

def _banding(self):
    self.ui.Login_PushButton.clicked.connect(self.Logined)
    self.ui.ShowPassword_ToolButton.clicked.connect(self.ShowPassword)

 对Login进行实现,获取name和passWord,与当前设定对比,如果相同,提示登录成功,如果不相同提示登录失败

def Logined(self):
    name = self.ui.Name_LineEdit.text()
    password = self.ui.Password_LineEdit.text()
    if (name == 'test' and password == '8888'):
      QMessageBox.information(self, '信息提示框',  '登录成功')
    else:
      QMessageBox.information(self, '信息提示框',  '登录失败')

对ShowPassword的button进行

  def ShowPassword(self):
    if self.hide_password:
      self.ui.Password_LineEdit.setEchoMode(QLineEdit.Password)
      self.ui.ShowPassword_ToolButton.setText(QCoreApplication.translate("Login_Form", u"+", None))
    else:
      self.ui.Password_LineEdit.setEchoMode(QLineEdit.Normal)
      self.ui.ShowPassword_ToolButton.setText(QCoreApplication.translate("Login_Form", u"-", None))
    self.hide_password = not self.hide_password

 全部代码

from PySide6 import QtWidgets
from PySide6.QtWidgets import QLineEdit, QMessageBox
from PySide6.QtCore import QCoreApplication

from login_ui import Ui_Login_Form

class Login(QtWidgets.QWidget):
  
  def __init__(self, parent=None):
    super(Login, self).__init__(parent)

    self.ui = Ui_Login_Form()
    self.hide_password = False
    self.ui.setupUi(self)
    self._banding()

  def _banding(self):
    self.ui.Login_PushButton.clicked.connect(self.Logined)
    self.ui.ShowPassword_ToolButton.clicked.connect(self.ShowPassword)

  def Logined(self):
    name = self.ui.Name_LineEdit.text()
    password = self.ui.Password_LineEdit.text()
    if (name == 'test' and password == '8888'):
      QMessageBox.information(self, '信息提示框',  '登录成功')
    else:
      QMessageBox.information(self, '信息提示框',  '登录失败')

  def ShowPassword(self):
    if self.hide_password:
      self.ui.Password_LineEdit.setEchoMode(QLineEdit.Password)
      self.ui.ShowPassword_ToolButton.setText(QCoreApplication.translate("Login_Form", u"+", None))
    else:
      self.ui.Password_LineEdit.setEchoMode(QLineEdit.Normal)
      self.ui.ShowPassword_ToolButton.setText(QCoreApplication.translate("Login_Form", u"-", None))
    self.hide_password = not self.hide_password


if __name__ == "__main__":
  app = QtWidgets.QApplication([])

  obj = Login()
  obj.show()
  app.exec()

5. PyInstall打包

安装pyinstalller

pip install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple/

PySide6起步_第4张图片

 

你可能感兴趣的:(PySide6,python,pyqt,ui)