跳转至

注册界面实现

登录注册界面

很多软件第一次使用都需要注册用户才能使用,注册用户需要填写基本信息,如用户名、密码、性别、爱好、个性签名,婚恋网站还需要填写择偶要求等

实现分析

整体布局可以采用竖直布局,用户名到个性签名可以采用表单布局方式,择偶要求一行可以采用水平布局方式实现

代码实现

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon
import sys

sex = ''
hobiList = []
def regist():
    global sex
    # 获取用户名
    name = nameEdit.text()
    pwd = pwdEdit.text()
    # 获取点击的性别
    if rb1.isChecked():
        sex = rb1.text()
    elif rb2.isChecked():
        sex = rb2.text()
    elif rb3.isChecked():
        sex = rb3.text()
    # 爱好
    if ck1.isChecked():
        hobiList.append(ck1.text())
    elif ck2.isChecked():
        hobiList.append(ck2.text())
    elif ck3.isChecked():
        hobiList.append(ck3.text())
    # 签名
    sig = sigEdit.text()
    # 择偶要求
    textEditMsg = textEdit.toPlainText()
    # 打印
    msg = '用户名:{},密码:{},性别:{},爱好:{},个性签名:{},择偶要求:{}'.format(name,
                                                            pwd,sex,hobiList,sig,textEditMsg)
    print(msg)




# 1.创建应用程序
app = QApplication(sys.argv)

# 2.创建窗口
w = QWidget()


# 修改窗口标题
w.setWindowTitle('注册登录')

"""------------------ 创建界面 ------------------"""
# 整体布局
wholeLayout = QVBoxLayout()
# 第一部分
firstLayout = QFormLayout()
# 第二部分
secondLayout = QHBoxLayout()
# 第三部分
regBtn = QPushButton('确认注册')
# 设置按钮的大小
regBtn.setFixedSize(100,30)

# 将整体的布局添加到窗口中
w.setLayout(wholeLayout)

# 添加三部分到整体布局中
wholeLayout.addLayout(firstLayout)
wholeLayout.addLayout(secondLayout)
wholeLayout.addWidget(regBtn,alignment=Qt.AlignHCenter)

# 第一部分控件
nameEdit= QLineEdit()
pwdEdit= QLineEdit()
sexLayout = QHBoxLayout()
rb1 = QRadioButton('男')
rb2 = QRadioButton('女')
rb3 = QRadioButton('妖')
rb1.setChecked(True)
sexLayout.addWidget(rb1)
sexLayout.addWidget(rb2)
sexLayout.addWidget(rb3)

hobiLayout = QHBoxLayout()
ck1 = QCheckBox('抽烟')
ck2 = QCheckBox('喝酒')
ck3 = QCheckBox('烫头')
hobiLayout.addWidget(ck1)
hobiLayout.addWidget(ck2)
hobiLayout.addWidget(ck3)

sigEdit = QLineEdit()

firstLayout.addRow('用户名',nameEdit)
firstLayout.addRow('密  码',pwdEdit)
firstLayout.addRow('性  别',sexLayout)
firstLayout.addRow('爱  好',hobiLayout)
firstLayout.addRow('个性签名',sigEdit)

# 第二部分
label = QLabel('择偶要求')
textEdit = QTextEdit()

secondLayout.addWidget(label)
secondLayout.addWidget(textEdit)

"""------------------ 注册事件 ------------------"""
regBtn.clicked.connect(regist)

# 3.显示窗口
w.show()

# 4.等待窗口停止
sys.exit(app.exec())