我在pyqt中写了一个小例子。它首先绘制一些文本并在下面添加三个按钮。但是按钮下方有一些空间。如何删除那些空间?
我试过addStrech(1)
了,但是文本消失了。
这是我的代码:
import sys
from PyQt4 import QtGui, QtCore
class CardWidget(QtGui.QWidget):
def __init__(self):
super(CardWidget, self).__init__()
self.initUI()
def initUI(self):
lButton = QtGui.QPushButton("left")
mButton = QtGui.QPushButton("middle")
rButton = QtGui.QPushButton("right")
sometext = DrawText()
hbox = QtGui.QHBoxLayout()
hbox.addWidget(lButton)
hbox.addStretch(1)
hbox.addWidget(mButton)
hbox.addStretch(1)
hbox.addWidget(rButton)
WButton = QtGui.QWidget()
WButton.setLayout(hbox)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(sometext)
vbox.addStretch(1)
vbox.addWidget(WButton)
self.setLayout(vbox)
self.setGeometry(300, 300, 480, 370)
self.setWindowTitle('Flashcards')
self.show()
class DrawText(QtGui.QWidget):
def __init__(self):
super(DrawText, self).__init__()
self.initUI()
def initUI(self):
self.text = 'some text'
self.setGeometry(0, 0, 200, 400)
#self.setWindowTitle('Draw text')
self.show()
def paintEvent(self, event):
qp = QtGui.QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end()
def drawText(self, event, qp):
qp.setPen(QtGui.QColor(168, 34, 3))
qp.setFont(QtGui.QFont('Decorative', 10))
qp.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)
def main():
app = QtGui.QApplication(sys.argv)
ex = CardWidget()
sys.exit(app.exec_())
if __name__ == '__main__':
main()