Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyqt5 layout
#3
Some suggestions on using Qt.
import sys
### Avoid using from *** import ***.  Import the package and use package.thing
### I find this makes it a lot easier to find the Qt specific parts of a program.
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;'

### Give classes names that describe what they are.  Give functions and methods 
### names that describe what they do
class DateTimeDisplay(QtWidgets.QWidget):
    def __init__(self, *args, **kvargs):
        super().__init__(*args, **kvargs)
        ### Avoid set specific sizes and locations for widgets.
        ### to establish the initial size
        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))
        ### Use layout managers to position widgets
        ### To size labels, provide representative text to be used
        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)
        ### In most instances the central widget of a main window will be a QWidget or
        ### a user created class that is subclassed from QWidget.  This is an example
        ### of placing multiple widgets inside the QMainWindow
        content = QtWidgets.QWidget()
        datetime_display = DateTimeDisplay()
        layout = QtWidgets.QVBoxLayout(content)
        layout.addWidget(QtWidgets.QLabel('This is my cool date time display widget'))
        layout.addWidget(datetime_display)
        layout.addWidget(QtWidgets.QLabel('Have a nice day'))
        self.setCentralWidget(content)

        ### Avoid embedding usage details in a class.  Make the class as generic
        ### as possible and give the user flexibility on how the class is used.
        update = QtCore.QTimer(self)
        update.timeout.connect(datetime_display.update)
        update.start(1000)

App = QtWidgets.QApplication(sys.argv)
window = Window()

### Limit using stylesheets.  Apply styles to the entire application when
### possible instead of styling individual widgets.
window.setStyleSheet(stylesheet)
window.show()
App.exit(App.exec_())
None of these suggestions are rules. I wouldn't even call them guidelines, just suggestions.
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 675 Nov-10-2023, 01:52 PM
Last Post: Axel_Erfurt
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,874 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  Python GUI layout off between different OS shift838 5 3,797 Jan-02-2019, 02:53 AM
Last Post: shift838
  PyQt5: Add Variable Number of Rows to Layout Based on Python Dictionary kennybassett 2 4,823 Oct-02-2018, 02:05 PM
Last Post: Alfalfa
  [Tkinter] grid layout neech 8 17,846 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