综合运用
登陆窗体。
方法一:运用 QFormLayout 布局。
def lOgin():
def getB(i):
tc = QPushButton(i)
tc.setMaximumWidth(100)
return tc
hl = QHBoxLayout()
for i in ['登陆', '退出']:
hl.addWidget(getB(i))
hl.insertSpacing(1, 10)
# hl.setAlignment(Qt.AlignCenter)
def getL(i):
lb = QLabel(i)
le = QLineEdit()
le.setMaximumWidth(150)
return lb, le
fl = QFormLayout(self)
for i in ['用户名:', '密 码:']:
lb, le = getL(i)
fl.addRow(lb, le)
fl.addRow(QHBoxLayout())
fl.addRow(hl)
fl.setFormAlignment(Qt.AlignCenter)
fl.setVerticalSpacing(10)
问题:虽然使用了 fl.setFormAlignment(Qt.AlignCenter)方法让他居中整个Form表单,但是按钮加到行里面就偏左了。感觉没办法调整了,接下来换个方法试试。
方法二:运营 QHBoxLayout + QVBoxLayout
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, \
QLineEdit, QMessageBox, QLabel, QVBoxLayout, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
class MyClass(QWidget):
def __init__(self):
super(MyClass, self).__init__()
self.initUi()
self.show()
def initUi(self):
self.setWindowTitle('Login')
self.setWindowIcon(QIcon("More_threads_qt01/lizi.png"))
self.setGeometry((app.desktop().width() - self.width()) / 2, 300, 300, 200)
self.setWindowFlags(Qt.WindowCloseButtonHint)
def lOgin():
def btClick():
if self.sender().text() == '登陆':
QMessageBox(QMessageBox.Information,
'消息',
f'{ls[0][0]}{usle.text()},{ls[0][1]}{psle.text()}',
QMessageBox.Ok,
self).show()
else:
QApplication.instance().exit()
def getB(i):
tc = QPushButton(i)
tc.clicked.connect(btClick)
tc.setMinimumWidth(90)
return tc
hl = QHBoxLayout()
hl.addStretch()
for i in ['登陆', '退出']:
hl.addWidget(getB(i))
hl.addStretch()
hl.insertSpacing(1, 10)
def getL(k, v):
hl1 = QHBoxLayout()
hl1.addStretch()
hl1.addWidget(QLabel(k))
le = v
le.setMaximumWidth(150)
if k == '密 码:':
le.setEchoMode(QLineEdit.Password)
hl1.addWidget(le)
hl1.addStretch()
return hl1
vl = QVBoxLayout(self)
usle, psle = QLineEdit(), QLineEdit()
ls = [['用户名:', '密 码:'], [usle, psle]]
for k, v in zip(ls[0], ls[1]):
vl.addLayout(getL(k, v))
vl.addSpacing(10)
vl.addSpacing(5)
vl.addLayout(hl)
vl.setAlignment(Qt.AlignCenter)
lOgin()
if __name__ == '__main__':
# 新建一个app(进程)
app = QApplication(sys.argv)
# 初始化基类
myc = MyClass()
# 监控app的关闭键
sys.exit(app.exec_())
两个对其方法:
1、stratch();水平布局中加弹簧。
2、vl.setAlignment(Qt.AlignCenter);垂直布局里使用居中的对齐方法。
这样能更好的达到居中的效果。
弹窗方法,需要注意事件获取对象的时候要让对象在事件函数外获取到,不能放在自己生成界面的函数里面,否则事件函数获取不到。
PicstoLoop图片轮播——QtCore.QTimer
貌似比QtCore.QBasicTimer更灵活。
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPixmap, QIcon
class MyClass(QWidget):
def __init__(self):
super(MyClass, self).__init__()
self.initUi()
self.show()
def initUi(self):
self.setWindowTitle('PIcs Loop')
self.setWindowIcon(QIcon("More_threads_qt01/lizi.png"))
self.setGeometry((app.desktop().width() - self.width()) / 2, 300, 400, 300)
self.setWindowFlags(Qt.WindowCloseButtonHint)
def picsLoop():
vl = QVBoxLayout(self)
lbt = QLabel()
lbt.setText('PIcs Loop')
lbt.setAlignment(Qt.AlignHCenter)
vl.addWidget(lbt)
lb = QLabel()
self.n = 1
lb.setPixmap(QPixmap(f'More_threads_qt01/imgs/emo{self.n}.jpg'))
lb.setScaledContents(True)
lb.setFixedSize(200, 200)
vl.addSpacing(20)
vl.addWidget(lb)
vl.setAlignment(Qt.AlignCenter)
timer1 = QTimer(self)
def tiMeout():
self.n += 1
if self.n > 4:
self.n = 1
lb.setPixmap(QPixmap(f'More_threads_qt01/imgs/emo{self.n}.jpg'))
timer1.timeout.connect(tiMeout)
timer1.start(1000)
picsLoop()
if __name__ == '__main__':
# 新建一个app(进程)
app = QApplication(sys.argv)
# 初始化基类
myc = MyClass()
# 监控app的关闭键
sys.exit(app.exec_())
QSpinBox
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QSpinBox, QDoubleSpinBox
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
class MyClass(QWidget):
def __init__(self):
super(MyClass, self).__init__()
self.initUi()
self.show()
def initUi(self):
self.setWindowTitle('QSpinBox测试')
self.setWindowIcon(QIcon("More_threads_qt01/lizi.png"))
self.setGeometry((app.desktop().width() - self.width()) / 2, 300, 400, 300)
self.setWindowFlags(Qt.WindowCloseButtonHint)
def qSpin():
hl = QHBoxLayout(self)
hl.setAlignment(Qt.AlignTop)
for o, r, q, h in zip(
[QSpinBox(), QDoubleSpinBox()],
[100, 30],
['物品 ', '单价 '],
[' 件', ' 元']):
o.setRange(0, r)
o.setPrefix(q)
o.setSuffix(h)
if o.text() == '单价 0.00 元':
o.setSingleStep(0.5)
hl.addWidget(o)
qSpin()
if __name__ == '__main__':
# 新建一个app(进程)
app = QApplication(sys.argv)
# 初始化基类
myc = MyClass()
# 监控app的关闭键
sys.exit(app.exec_())
QFormLayout & addWidget(QLabel(), 0, Qt.AlignCenter)布局方法
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QVBoxLayout, QLineEdit, QLabel, QPushButton
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
class MyClass(QWidget):
def __init__(self):
super(MyClass, self).__init__()
self.initUi()
self.show()
def initUi(self):
self.setWindowTitle('Regis')
self.setWindowIcon(QIcon("More_threads_qt01/lizi.png"))
self.setGeometry(app.desktop().width() / 2, 500, 300, 140)
self.setWindowFlags(Qt.WindowCloseButtonHint)
def qSpin():
vl = QVBoxLayout(self)
vl.setAlignment(Qt.AlignTop)
vl.addWidget(QLabel('欢迎进入注册系统'), 0, Qt.AlignCenter)
ql = QFormLayout()
tel = QLineEdit()
for t, o in zip(['手机号码:', '密码:'], [tel, QLineEdit()]):
ql.addRow(t, o)
if t == '密码:':
o.setEchoMode(QLineEdit.Password)
vl.addLayout(ql)
btn = QPushButton('提交')
def bTn():
list1.append(my_newClass(tel.text()))
btn.clicked.connect(bTn)
vl.addWidget(btn)
vl.setSpacing(10)
qSpin()
class my_newClass(QWidget):
def __init__(self, t):
super().__init__()
self.initUI(t)
self.show()
def initUI(self, t):
self.setWindowTitle('显示结果')
self.setWindowFlags(Qt.WindowCloseButtonHint)
self.resize(100, 50)
QVBoxLayout(self).addWidget(QLabel(f'注册成功:{t}'), 0, Qt.AlignCenter)
if __name__ == '__main__':
# 新建一个app(进程)
app = QApplication(sys.argv)
# 初始化基类
myc = MyClass()
# 建一个数组装新窗体
list1 = []
# 监控app的关闭键
sys.exit(app.exec_())