I'm using PyQt4 to make a Application and on the window their is a datetime-label( live updating). I Wan't to put this label into a Hboxlayout, together with two other QLabels. This code is used:
import sys,os
from PyQt4 import QtGui,QtCore
from label_datetime import *
class ApplicationWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.main_widget = QtGui.QWidget(self)
layout=QtGui.QGridLayout(self.main_widget)
layout.setSpacing(10)
self.datetime=label_datetime()
self.linkertitel=QtGui.QLabel('Uurbasis')
self.rechtertitel=QtGui.QLabel('Dagbasis')
qf=QtGui.QFont("Arial",20)
self.linkertitel.setFont(qf)
self.rechtertitel.setFont(qf)
self.rechtertitel.setStyleSheet('color: #d3d3d3; border:1px solid rgb(0, 255, 0)')
self.linkertitel.setStyleSheet('color: #d3d3d3; border:1px solid rgb(0, 255, 0)')
self.linkertitel.setAlignment(QtCore.Qt.AlignCenter)
self.rechtertitel.setAlignment(QtCore.Qt.AlignCenter)
layout_titel=QtGui.QHBoxLayout()
layout_titel.addWidget(self.linkertitel)
layout_titel.addWidget(self.datetime)
layout_titel.addWidget(self.rechtertitel)
self.groep_titel=QtGui.QGroupBox()
self.groep_titel.setLayout(layout_titel)
layout.addWidget(self.groep_titel,1,1,1,10)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
QtGui.QShortcut(QtGui.QKeySequence("Ctrl+Q"), self, self.close)
qApp = QtGui.QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("my app")
aw.showFullScreen()
aw.show()
sys.exit(qApp.exec_())
And my code for the label_datetime:
from PyQt4 import QtGui,QtCore
from PyQt4.Qt import QFont, QColor
class label_datetime(QtGui.QWidget):
def __init__(self):
super(label_datetime,self).__init__()
self.label = QtGui.QLabel('tijd',self)
qf=QtGui.QFont("Arial",20)
self.label.setFont(qf)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.adjustSize()
self.setStyleSheet('color: #d3d3d3; border:1px solid rgb(0, 255, 0)')
timer=QtCore.QTimer(self)
timer.timeout.connect(self.displaytime)
timer.start(1000)
self.show()
def displaytime(self):
tijd=QtCore.QDateTime.currentDateTime().toString()
self.label.setText(QtCore.QDateTime.currentDateTime().toString())
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.adjustSize()
I want the three labels to be in the center of the given space. however this datetime label doesn't want to resize. The result is this:
If I remove self.label.adjustSize(), and add: self.label.setSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding), the Qlabel shrinks and only half of the date is visibe.The size of my datetime lable doesn't want to change. Does someone know what I'm doing wrong ?
The problem is you're expecting a QtGui.Widget
behaves as a QtGui.QLabel
, your label_datetame
class is not a QLabel
, is a QWidget
since its inherits from it.
You should inherit your class from QLabel
in order allow the other widgets ( QLayout
, QWindow
, etc...) "talk" to it the "QLabel languaje". A QLabel
has functions, methods and properties a QWidget
hasn't.
Change your class to this:
class label_datetime(QtGui.QLabel):
def __init__(self, parent=None):
super(label_datetime,self).__init__(parent)
qf = QtGui.QFont("Arial", 20)
self.setFont(qf)
self.setAlignment(QtCore.Qt.AlignCenter)
self.adjustSize()
self.setStyleSheet('color: #d3d3d3; border:1px solid rgb(0, 255, 0)')
timer = QtCore.QTimer(self)
timer.timeout.connect(self.displaytime)
timer.start(1000)
self.show()
def displaytime(self):
tijd = QtCore.QDateTime.currentDateTime().toString()
self.setText(QtCore.QDateTime.currentDateTime().toString())
self.setAlignment(QtCore.Qt.AlignCenter)
self.adjustSize()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.