PyQt的TextEdit和PushButton控件
环境 pyqt5+VSCode
在Qt Designer中拖拽Label、Line Edit、Push Button到界面中如图:
它们名字是label、label_2、lineEdit、lineEdit_2、pushbutton
保存为pyqt2.ui。compile成.py文件后,新建文件pyqt2.py。
import sys
from Ui_pyqt2 import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import QtWidgets
class MyMainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None): #parent 参数
super(MyMainWindow, self).__init__(parent)
self.setupUi(self)
self.label.setText("用户名:") #设置label文本
self.label_2.setText("密码:")
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password) #密码模式,QtWidgets.QLineEdit.Normal是正常模式
self.pushButton.setText("确认") #是指按键文本
self.pushButton.clicked.connect(self.func1) #连接信号与槽
def func1(self):
print("用户名:"+self.lineEdit.text()+" 密码:"+self.lineEdit_2.text())
app = QApplication(sys.argv)
win = MyMainWindow()
win.show()
sys.exit(app.exec_())
运行得到
LineEdit
方法
.setText() 设置文本
.text() 获得文本
.setEchoMode(QtWidgets.QLineEdit.Password) 显示模式,密码模式,QtWidgets.QLineEdit.Normal正常模式
.clear() 清除文本
.setFocus() 获得焦点
信号
textChanged() 改变文本框内容时
editingFinished() 按下Enter时或失去焦点时
Pushbutton
方法
.setText() 设置文本
.text() 获得文本
信号
clicked 点击时