import sys
from PyQt5.QtCore import*
from PyQt5.QtWidgets import QWidget, QApplication, QGroupBox, QPushButton, QLabel, QHBoxLayout, QVBoxLayout, QGridLayout, QFormLayout, QLineEdit, QTextEdit
from PyQt5.QtGui import QIcon
class Stream(QThread):
newText = pyqtSignal(str)
def write(self, text):
self.newText.emit(str(text))
class login(QWidget):
def init(self):
super().__init__()
sys.stdout = Stream(newText=self.onUpdateText)
self.initUi()
self.initUI2()
self.show()
#
def onUpdateText(self, text):
# Write console output to text widget.
cursor = self.process.textCursor()
cursor.movePosition(QTextCursor.End)
cursor.insertText(text)
self.process.setTextCursor(cursor)
self.process.ensureCursorVisible()
def closeEvent(self, event):
"""Shuts down application on close."""
# Return stdout to defaults.
sys.stdout = sys.__stdout__
super().closeEvent(event)
#
def initUi(self):
self.setWindowTitle("login")
layout = QGridLayout()
self.setGeometry(600, 600, 400, 400)
btnGenMast = QPushButton('Run', self)
btnGenMast.clicked.connect(self.genMastClicked)
self.process = QTextEdit(self, readOnly=True)
self.process.ensureCursorVisible()
self.process.setLineWrapColumnOrWidth(500)
self.process.setLineWrapMode(QTextEdit.FixedPixelWidth)
nameLabel = QLabel("姓名")
self.nameLineEdit = QLineEdit(" ")
sexLabel = QLabel("性别")
self.sexLineEdit = QLineEdit(" ")
emitLabel = QLabel("手机号")
self.phoneLineEdit = QLineEdit("")
timeLabel = QLabel("邮箱")
self.mailEdit = QLineEdit("")
# layout.setSpacing(10)
layout.addWidget(nameLabel,1,0)
layout.addWidget(self.nameLineEdit,1,1)
layout.addWidget(sexLabel, 2, 0)
layout.addWidget(self.sexLineEdit, 2, 1)
layout.addWidget(emitLabel,3,0)
layout.addWidget(self.phoneLineEdit,3,1)
layout.addWidget(timeLabel,4,0)
layout.addWidget(self.mailEdit,4,1)
layout.setColumnStretch(1, 10)
save_Btn = QPushButton('保存')
cancle_Btn = QPushButton('取消')
cancle_Btn.clicked.connect(QCoreApplication.quit)
save_Btn.clicked.connect(self.addNum)
layout.addWidget(save_Btn)
layout.addWidget(cancle_Btn)
layout.addWidget(btnGenMast)
layout.addWidget(self.process)
self.setLayout(layout)
def initUI2(self):
self.setWindowIcon(QIcon('1.png'))
def printhello(self):
print('hello')
def genMastClicked(self):
print('Running...')
self.printhello()
loop = QEventLoop()
QTimer.singleShot(2000, loop.quit)
loop.exec_()
print('Done.')
def addNum(self): # 获取文本框内容
name = self.nameLineEdit.text()
sex = self.sexLineEdit.text()
phone = self.phoneLineEdit.text()
mail = self.mailEdit.text()
print('姓名: %s 性别: %s 手机号: %s 邮箱: %s ' % (name,sex, phone, mail))
if name == '__main__':
app = QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
gui= login()
sys.exit(app.exec_())