PyQt5学习5-行编辑

QLineEdit

QLineEdit编辑一行的字符

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
#!/usr/bin/python3
#-*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QWidget,QApplication, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox
class ex(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
vbox = QHBoxLayout()
lb = QLabel('Enter you name')
self.le = QLineEdit()
bt = QPushButton('Hello')
bt.clicked.connect(self.hello)
vbox.addWidget(lb)
vbox.addWidget(self.le)
vbox.addWidget(bt)
self.setLayout(vbox)
self.setWindowTitle('Hello')
self.resize(400,300)
self.show()
def hello(self):
QMessageBox.information(self,'Information','Hello '+ self.le.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
e = ex()
sys.exit(app.exec_())

该程序产生如下窗口

QLineEdit-1

在QLineEdit框中输入字符后,点击 Hello, 出现一个信息框

QLineEdit-2

QLabel

QLabel 显示图片或字符。

QComboBox

QComboBox是下拉列表框,通过下拉列表选取需要的选项。

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
#!/usr/bin/python3
#-*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QWidget,QApplication, QGridLayout, QLabel, QLineEdit, QComboBox
class ex(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
grid = QGridLayout()
self.setLayout(grid)
self.lb = QLabel('System')
self.le = QLineEdit()
combo = QComboBox(self)
combo.addItem('Windows')
combo.addItem('MacOS')
combo.addItem('Linux')
combo.activated[str].connect(self.onActivated)
grid.addWidget(self.lb,0,0)
grid.addWidget(combo,0,1)
grid.addWidget(self.le,1,0,1,2)
self.setWindowTitle('combo')
self.show()
def onActivated(self,text):
self.le.setText(text)
if __name__ == '__main__':
app = QApplication(sys.argv)
e = ex()
sys.exit(app.exec_())

该程序产生一个窗口如下

w3

通过下拉列表选择系统,并在QLineEdit中显示所选的内容。