首页 > 代码库 > pyqt label窗口变化
pyqt label窗口变化
#_*_ coding: utf-8 _*_
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))
class FindDialog(QDialog):
def __init__(self,parent = None):
super(FindDialog, self).__init__(parent)
self.setWindowTitle(self.tr("Find"))
# QLabel控件中显示的图像和文字的位置可以用setAlignment和setIndent方法调整。文字内容也可以用setWordWrap方法分成几行。
label = QLabel(self.tr("Find &what:"))
lineEdit = QLineEdit()
# setBuddy方法可以将QLabel控件与QLineEdit控件联系起来,从而将键盘焦点转移到QLineEdit控件上
label.setBuddy(lineEdit)
caseCheckBox = QCheckBox(self.tr("Match &case"))
backwardCheckBox = QCheckBox(self.tr("Search &backward"))
self.findButton = QPushButton(self.tr("&Find"))
self.findButton.setDefault(True)
self.findButton.setEnabled(False)
closeButton = QPushButton(self.tr("Close"))
QObject.connect(lineEdit,SIGNAL("textChanged(const QString &)"),self.enableFindButton)
QObject.connect(self.findButton, SIGNAL("clicked()"), self.findClicked)
QObject.connect(closeButton, SIGNAL("clicked()"), self, SLOT("close()"))
topLeftLayout = QHBoxLayout()
topLeftLayout.addWidget(label)
topLeftLayout.addWidget(lineEdit)
leftLayout = QVBoxLayout()
leftLayout.addLayout(topLeftLayout)
leftLayout.addWidget(caseCheckBox)
leftLayout.addWidget(backwardCheckBox)
rightLayout = QVBoxLayout()
rightLayout.addWidget(self.findButton)
rightLayout.addWidget(closeButton)
rightLayout.addStretch()
mainLayout = QHBoxLayout()
mainLayout.addLayout(leftLayout)
mainLayout.addLayout(rightLayout)
self.setLayout(mainLayout)
self.setFixedHeight(self.sizeHint().height())
# 自定义TAB键的顺序,只两个参数不方便
# QWidget.setTabOrder(closeButton,self.findButton)
#QWidget.setTabOrder(self.findButton,backwardCheckBox)
def findNext(str,cs):
pass
def findPrevious(str,cs):
pass
def findClicked(self):
text = lineEdit.text()
if caseCheckBox.isChecked():
cs = Qt.CaseSensitive
else:
cs = Qt.CaseInsensitive
if backwardCheckBox.isChecked():
self.findPrevious.emit(text,cs)
else: self.findNext.emit(text,cs)
def enableFindButton(self, text):
self.findButton.setEnabled(not text.isEmpty())
app = QApplication(sys.argv)
aaa = FindDialog()
aaa.show()
app.exec_() # 测试ALT+W, ALT+C, ALT+B, ALT+F,默认的TAB键是控制创建的顺序 # QWidget::setTabOrder()可以改变这个顺序 # connect(lineEdit, SIGNAL(textChanged(const QString &)),this, SIGNAL(updateRecord(const QString &)));
如图:
pyqt label窗口变化