Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyqt5 layout
#8
You should use layout managers instead of manually positioning the widget. Qt for C++ is so cloe to Python Qt that all help for C++ is applicable to Qt5 or PySide2/6
import sys
import PySide2.QtCore as QtCore
import PySide2.QtGui as QtGui
import PySide2.QtWidgets as QtWidgets
from PySide2.QtCore import Qt
 
font = "Arial"
stylesheet = 'background-color:black; color:antiquewhite;'
 
class DateTimeDisplay(QtWidgets.QWidget):
    def __init__(self, *args, **kvargs):
        super().__init__(*args, **kvargs)
        self.time = QtWidgets.QLabel(
            '12:00 AM',
            font=QtGui.QFont(font, 60))
        self.date = QtWidgets.QLabel(
            'Wednesday, December 31, 2020',
            font=QtGui.QFont(font, 30))
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.time)
        layout.addWidget(self.date)
        self.update()
  
    def update(self):
        """Call to update date and time display"""
        text = QtCore.QTime.currentTime().toString(Qt.DefaultLocaleShortDate)
        self.time.setText(text)
        text = QtCore.QDate.currentDate().toString(Qt.DefaultLocaleLongDate)
        self.date.setText(text)
  
class Window(QtWidgets.QMainWindow):
    def __init__(self, *args, **kvargs):
        super().__init__(*args, **kvargs)
        content = QtWidgets.QWidget()
        datetime_display = DateTimeDisplay()
        layout = QtWidgets.QGridLayout(content)
        layout.addWidget(QtWidgets.QLabel('Date/Time'), 0, 0)
        layout.addWidget(datetime_display, 0, 1)
        layout.addWidget(
            QtWidgets.QLabel('Resize window and date time widget stays in upper right'),
            1, 0, 1, 2)
        layout.setColumnStretch(0, 1)
        layout.setRowStretch(1, 1)
        self.setCentralWidget(content)
 
        update = QtCore.QTimer(self)
        update.timeout.connect(datetime_display.update)
        update.start(1000)
 
App = QtWidgets.QApplication(sys.argv)
window = Window() 
window.setStyleSheet(stylesheet)
window.show()
App.exit(App.exec_())
Reply


Messages In This Thread
pyqt5 layout - by Nickd12 - Jan-12-2021, 03:06 AM
RE: pyqt5 layout - by Axel_Erfurt - Jan-12-2021, 09:26 AM
RE: pyqt5 layout - by deanhystad - Jan-12-2021, 04:35 PM
RE: pyqt5 layout - by Nickd12 - Jan-12-2021, 11:20 PM
RE: pyqt5 layout - by Nickd12 - Jan-12-2021, 11:26 PM
RE: pyqt5 layout - by deanhystad - Jan-13-2021, 03:38 AM
RE: pyqt5 layout - by Nickd12 - Jan-13-2021, 04:07 AM
RE: pyqt5 layout - by deanhystad - Jan-18-2021, 04:45 AM
RE: pyqt5 layout - by Axel_Erfurt - Jan-18-2021, 09:09 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 Relative Layout Unkovic 1 705 Nov-10-2023, 01:52 PM
Last Post: Axel_Erfurt
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,911 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  Python GUI layout off between different OS shift838 5 3,838 Jan-02-2019, 02:53 AM
Last Post: shift838
  PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary kennybassett 2 4,858 Oct-02-2018, 02:05 PM
Last Post: Alfalfa
  [Tkinter] grid layout neech 8 17,952 Oct-14-2016, 07:06 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020