Answer the question
In order to leave comments, you need to log in
How to insert a time label into a widget?
Can't insert time label into left widget, please help
Here is an example code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(). __init__() #main window
size
self.resize(845, 535)
self.setMinimumSize(QtCore.QSize(845, 535))
self.setMaximumSize(QtCore.QSize(845, 535))
self.setStyleSheet("background-color: rgb(45, 45, 45);")
#centralwidget
self.centralwidget = QtWidgets.QWidget()
self.centralwidget.setObjectName("centralwidget")
self.setCentralWidget(self.centralwidget)
self.Content = QtWidgets.QFrame(self.centralwidget)
self.Content.setStyleSheet("background-color: rgb(255, 255, 255);")
self.Content.setFrameShape(QtWidgets. QFrame.NoFrame)
self.Content.setFrameShadow(QtWidgets.QFrame.Raised)
self.Content.setObjectName("Content")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.Content)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setSpacing(0) #left
widget
self.frame_left_menu = QtWidgets.QFrame(self.Content)
self.frame_left_menu.setMinimumSize(QtCore.QSize(45, 0))
self.frame_left_menu.setStyleSheet
(
"background-color: rgb(255, 255, 255);"
)
self.frame_left_menu.setObjectName("frame_left_menu")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.frame_left_menu)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0) #not sure
why
self.frame_top_menus = QtWidgets.QFrame(self .frame_left_menu)
self.frame_top_menus.setFrameShape(QtWidgets.QFrame.NoFrame)
self.frame_top_menus.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_top_menus.setObjectName("frame_top_menus") #set
vertical alignment
self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.frame_top_menus)
self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_4.setSpacing(0 )
#house
self.btn_page_1 = QtWidgets.QPushButton("", self.frame_top_menus)
self.btn_page_1.setIcon(QIcon('home.png'))
self.btn_page_1.setIconSize(QSize(45,45))
self.btn_page_1.setMinimumSize(QtCore .QSize(0, 20))
self.btn_page_1.setStyleSheet("QPushButton {\n"
"border: 0px solid;\n"
"}\n"
"}")
self.btn_page_1.setObjectName("btn_page_1")
self.verticalLayout_4.addWidget(self.btn_page_1)
#Notes
self.btn_page_2 = QtWidgets.QPushButton("", self.frame_top_menus)
self.btn_page_2.setIcon(QIcon('note1.png' ))
self.btn_page_2.setIconSize(QSize(45,45))
self.btn_page_2.setMinimumSize(QtCore.QSize(0, 40))
self.btn_page_2.setStyleSheet("QPushButton {\n"
"border: 0px solid;\n "
"}\n"
"}")
self.btn_page_2.setObjectName("btn_page_2") #create
widget
self.verticalLayout_4.addWidget(self.btn_page_2)
#weather
self.btn_page_3 = QtWidgets.QPushButton("", self.frame_top_menus)
self.btn_page_3.setIcon(QIcon('sun.png'))
self.btn_page_3.setIconSize(QSize(45,45))
self.btn_page_3.setMinimumSize(QtCore .QSize(0, 40))
self.btn_page_3.setStyleSheet("QPushButton {\n"
" border: 0px solid;\n"
"}\n"
"}")
timer = QTimer(self)
timer.timeout.connect( showTime)
timer.start(1000)
font1 = QFont('Arial', 8, QFont.Bold) #trying
to set the time inside the left frame
self.timewidget = QtWidgets.QLabel(showTime, self.frame_top_menus)
self.timewidget.setMinimumSize(QtCore.QSize(45,45))
self.timewidget.setAlignment(Qt.AlignBottom)
self.timewidget.setFont(font1) #function
to show time
def showTime(self):
current_time = QtCore.QTime.currentTime()
label_time = current_time.toString('hh:mm')
self.timewidget.setText(label_time)
self.btn_page_3.setObjectName("btn_page_3")
self.verticalLayout_4.addWidget(self.btn_page_3) #specify
notes in the left frame
self.verticalLayout_3.addWidget(self.frame_top_menus, 0, QtCore.Qt. AlignTop)
self.horizontalLayout_2.addWidget(self.frame_left_menu)
self.frame_pages = QtWidgets.QFrame(self.Content)
self.frame_pages.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_pages.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_pages.setObjectName("frame_pages")
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.frame_pages)
self.verticalLayout_5. setContentsMargins(0, 0, 0, 0)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.stackedWidget = QtWidgets.QStackedWidget(self.frame_pages)
self.stackedWidget.setMinimumSize(QtCore.QSize(800,535))
self.stackedWidget.setStyleSheet ('QStackedWidget{\n'
'border: 0px solid;')
self.stackedWidget.setObjectName("stackedWidget")
#1 page
self.page_1 = QtWidgets.QWidget()
self.page_1.setObjectName("page_1")
self.verticalLayout_7 = QtWidgets.QVBoxLayout(self.page_1)
self.verticalLayout_7.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_7.setObjectName ("verticalLayout_7") #specify
what will be on page 1
self.label_1 = QtWidgets.QLabel("Page1", self.page_1)
font = QtGui.QFont()
font.setPointSize(40)
self.label_1.setFont(font)
self.label_1.setStyleSheet("color: #FFF; background-color: #ba135d;")
self.label_1.setAlignment(QtCore.Qt.AlignCenter)
self.label_1.setObjectName("label_1")
self.verticalLayout_7.addWidget(self.label_1)
self.stackedWidget.addWidget(self.page_1)
#2 page
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.verticalLayout_6 = QtWidgets.QVBoxLayout( self.page_2)
self.verticalLayout_6.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_6.setObjectName("verticalLayout_6") #specify
what will be in page 2
self.label_2 = QtWidgets.QLabel("Page2", self. page_2)
font = QtGui.QFont()
font.setPointSize(40)
self.label_2.setFont(font)
self.label_2.setStyleSheet("color: #FFF; background-color: #1eae98;")
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.label_2.setObjectName("label_2")
self.verticalLayout_6.addWidget(self .label_2)
self.stackedWidget.addWidget(self.page_2)
#3 page
self.page_3 = QtWidgets.QWidget()
self.page_3.setObjectName("page_3")
self.verticalLayout_8 = QtWidgets.QVBoxLayout(self.page_3)
self.verticalLayout_8 .setContentsMargins(0, 0, 0, 0)
self.verticalLayout_8.setObjectName("verticalLayout_8" ) #specify
what will be on page 3
self.label = QtWidgets.QLabel("Page3", self.page_3)
font = QtGui.QFont()
font.setPointSize(0)
self.label.setFont(font)
self.label.setStyleSheet("color: #FFF; background -color: black;")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.verticalLayout_8.addWidget(self.label)
self.stackedWidget.addWidget(self.page_3)
self .verticalLayout_5.addWidget(self.stackedWidget)
self.horizontalLayout_2.addWidget(self.frame_pages)
self.stackedWidget.setCurrentIndex(0)
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.addWidget(self.Content) #Clicking
home
self.btn_page_1.clicked.connect(
lambda: self.stackedWidget. setCurrentWidget(self.page_1)) #Clicking
on notes
self.btn_page_2.clicked.connect(
lambda: self.stackedWidget.setCurrentWidget(self.page_2)) #Clicking
on weather
self.btn_page_3.clicked.connect(
lambda: self.stackedWidget. setCurrentWidget(self.page_3))
self.frame_left_menu.installEventFilter(self)
# function to animate the popup widget on hover
def eventFilter(self, obj, event):
return super().eventFilter(obj, event)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
app.setFont(QtGui.QFont("Times", 20, QtGui.QFont.Bold))
w = MainWindow()
w.show()
sys.exit(app.exec_())
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question