from PyQt5.QtWidgets import (QWidget
, QVBoxLayout , QHBoxLayout,
QLineEdit, QPushButton)
from PyQt5.QtCore import pyqtSignal
from PyQt5 import QtCore
##############################################
# 參考大丸子的博客
#http://jimmykuu.sinaapp.com/blog/11
class LoginView(QWidget):
## 登錄界面時發送 關閉信號
quitSignal = pyqtSignal()
loginSignal = pyqtSignal(list)
# server端沒有響應
openFailureSignal = pyqtSignal()
def __init__(self, parent=None):
super(LoginView, self).__init__(parent)
self.ids_receive = []
self._init_ui()
def _init_ui(self):
layout_button = QHBoxLayout()
layout_input = QHBoxLayout()
self.input_name = QLineEdit()
self.input_pass = QLineEdit()
button_login = QPushButton("登錄")
button_login.setObjectName("ok_button")
button_quit = QPushButton("取消")
button_hello = QPushButton("hello")
self.button_hello = button_hello
button_hello.setObjectName("hello_button")
#button_login.clicked.connect(self.do_login)
#button_quit.clicked.connect(self.loginQuit)
layout_button.addWidget(button_login)
layout_button.addWidget(button_quit)
layout_input.addWidget(self.input_name)
layout_input.addWidget(self.input_pass)
layout_input.addWidget(self.button_hello)
layout_main = QVBoxLayout()
self.setLayout(layout_main)
layout_main.addLayout(layout_button)
layout_main.addLayout(layout_input)
# QMetaObject. connectSlotsByName(QObject)
#網上百度到的說明:其作用是如其名稱一樣,用來將QObject 里的子孫QObject的某些信號按照其objectName連接到相應的槽上
# ,如 button_hello.setObjectName("hello_button")
# 官網解釋用法: http://doc.qt.io/qt-5/qmetaobject.html#connectSlotsByName
QtCore.QMetaObject.connectSlotsByName(self)
def loginQuit(self):
print ("cencel")
#試試注銷掉該裝飾器
# 有點奇怪啊,一旦注釋掉裝飾器,打印會執行兩次啊
#QtCore.pyqtSlot(str, str)可以攜帶參數的
@QtCore.pyqtSlot()
def on_hello_button_clicked(self):
print('on_pbHello_clicked')
@QtCore.pyqtSlot()
def on_ok_button_clicked(self):
print ("OK")
self.loginSignal.emit([1, 2])
def do_login(self):
#獲取用戶和密碼文本框的內容
u_name = self.input_name.text()
u_pass = self.input_pass.text()
print('u_name', u_name, u_pass)
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
login = LoginView()
login.show()
sys.exit(app.exec_())