-
Notifications
You must be signed in to change notification settings - Fork 206
/
04-QLineEdit-登录案例.py
95 lines (77 loc) · 2.84 KB
/
04-QLineEdit-登录案例.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import sys
from PyQt5.Qt import *
class AccountTool:
ACCOUNT_ERROR = 0
PWD_ERROR = 1
SUCCESS = 2
@staticmethod # 设置为静态方法
def check_login(account, pwd):
# 把账号密码发送给服务器,等待服务器返回结果
if account != "muzing":
return AccountTool.ACCOUNT_ERROR
if pwd != "123456":
return AccountTool.PWD_ERROR
return AccountTool.SUCCESS
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("登录案例")
self.resize(500, 500)
self.setMinimumSize(400, 400)
# self.move(400, 250)
self.setup_ui()
def setup_ui(self):
# 添加三个控件
self.account_le = QLineEdit(self)
self.account_le.setPlaceholderText("请输入账号")
self.pwd_le = QLineEdit(self)
self.pwd_le.setPlaceholderText("请输入密码")
self.pwd_le.setEchoMode(QLineEdit.Password)
self.login_btn = QPushButton("登 录", self)
self.login_btn.clicked.connect(self.login_cao)
def login_cao(self):
account = self.account_le.text()
pwd = self.pwd_le.text()
state = AccountTool.check_login(account, pwd)
if state == AccountTool.ACCOUNT_ERROR:
print("账号错误")
self.account_le.setText("")
self.pwd_le.setText("")
self.account_le.setFocus()
return None
if state == AccountTool.PWD_ERROR:
print("密码错误")
self.pwd_le.setText("")
self.pwd_le.setFocus() # 让密码输入框获得焦点
return None
if state == AccountTool.SUCCESS:
print("登录成功")
# 优化前的代码
# if account == "muzing":
# if pwd == "123456":
# print("登录成功")
# else:
# print("密码错误")
# self.pwd_le.setText('')
# self.pwd_le.setFocus() # 让密码输入框获得焦点
# else:
# print("账号错误")
# self.account_le.setText('')
# self.pwd_le.setText('')
# self.account_le.setFocus()
def resizeEvent(self, evt) -> None:
widget_w = 180
widget_h = 38
margin = 40
self.account_le.resize(widget_w, widget_h)
self.pwd_le.resize(widget_w, widget_h)
self.login_btn.resize(widget_w, widget_h)
x = int((self.width() - widget_w) / 2)
self.account_le.move(x, int(self.height() / 5))
self.pwd_le.move(x, self.account_le.y() + self.account_le.height() + margin)
self.login_btn.move(x, self.pwd_le.y() + self.pwd_le.height() + margin)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())