pyqt5 layout - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: pyqt5 layout (/thread-31959.html) |
pyqt5 layout - Nickd12 - Jan-12-2021 for anyone that knows about pyqt5 how can i set my BuildDateTime class in a box to group it together then place it in the Qmainwindow the window class to as a group that way if i wanted to move the class as a whole i would be able to for example once i put date and time in the class i can just put them in a box and move the boxes position and not the elements themselves from PyQt5 import QtCore from PyQt5.QtCore import QDate, QTime, Qt from PyQt5.QtCore import (QTimer) from PyQt5.QtGui import * from PyQt5.QtGui import QFont from PyQt5.QtWidgets import * from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QLabel font = "Arial" color = "antiquewhite" style_sheet = f"color: {color} ;" class BuildDateTime(QWidget): def __init__(self): super().__init__() update = QTimer(self) update.timeout.connect(self.show_ui) update.start(1000) # The Time self.time = QLabel(self) self.time.setGeometry(0, 85, 400, 75) self.time.setStyleSheet(style_sheet) self.time.setFont(QFont(font, 60)) # The Date self.date = QLabel(self) self.date.setGeometry(0, 25, 700, 50) self.date.setStyleSheet(style_sheet) self.date.setFont(QFont(font, 30)) def show_ui(self): # Time get_time = QTime.currentTime() time_string = get_time.toString(Qt.DefaultLocaleShortDate) self.time.setText(time_string) # Date now = QDate.currentDate() date_string = now.toString(Qt.DefaultLocaleLongDate) self.date.setText(date_string) class Window(QMainWindow, BuildDateTime): def __init__(self): super().__init__() self.show() self.showFullScreen() self.setStyleSheet("background-color: black;") def keyPressEvent(self, e): if e.key() == QtCore.Qt.Key_Escape: self.close() if e.key() == QtCore.Qt.Key_F11: if self.isMaximized(): self.showNormal() else: self.showMaximized() def main_window(): # create pyqt5 app App = QApplication(sys.argv) # create the instance of our Window window = Window() # showing all the widgets window.show() # start the app App.exit(App.exec_()) main_window() RE: pyqt5 layout - Axel_Erfurt - Jan-12-2021 self.build_dt = BuildDateTime() and set it as Widget in mainWindow self.setCentralWidget(self.build_dt) from PyQt5.QtCore import (QDate, QTime, Qt, QTimer) from PyQt5.QtGui import QFont from PyQt5.QtWidgets import (QApplication, QMainWindow, QLabel, QWidget) import sys font = "Arial" color = "antiquewhite" style_sheet = f"color: {color} ;" class BuildDateTime(QWidget): def __init__(self): super().__init__() update = QTimer(self) update.timeout.connect(self.show_ui) update.start(1000) # The Time self.time = QLabel(self) self.time.setGeometry(0, 85, 400, 75) self.time.setStyleSheet(style_sheet) self.time.setFont(QFont(font, 60)) # The Date self.date = QLabel(self) self.date.setGeometry(0, 25, 700, 50) self.date.setStyleSheet(style_sheet) self.date.setFont(QFont(font, 30)) def show_ui(self): # Time get_time = QTime.currentTime() time_string = get_time.toString(Qt.DefaultLocaleShortDate) self.time.setText(time_string) # Date now = QDate.currentDate() date_string = now.toString(Qt.DefaultLocaleLongDate) self.date.setText(date_string) class Window(QMainWindow): def __init__(self): super().__init__() self.build_dt = BuildDateTime() self.setCentralWidget(self.build_dt) self.setGeometry(200, 200, 500, 200) #self.show() self.showFullScreen() self.setStyleSheet("background-color: black;") def keyPressEvent(self, e): if e.key() == Qt.Key_Escape: self.close() if e.key() == Qt.Key_F11: if self.isMaximized(): self.showNormal() else: self.showMaximized() if __name__ == '__main__': # create pyqt5 app App = QApplication(sys.argv) # create the instance of our Window window = Window() # showing all the widgets window.show() # start the app App.exit(App.exec_()) RE: pyqt5 layout - deanhystad - Jan-12-2021 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. RE: pyqt5 layout - Nickd12 - Jan-12-2021 @Alex_Erfurt thank you for reply now say i wanted to take that class and move it as a whole that way the date and time would stay in positions relative to one another but lets say i wanted to move them together left right or in the middle even i tried using self.build_dt.move() however it stayed in the same position how could i go about doing this? RE: pyqt5 layout - Nickd12 - Jan-12-2021 also @deanhystad thank you for the suggestions on pyqt im still learning how to work with it i have never made a gui before so i appreciate it RE: pyqt5 layout - deanhystad - Jan-13-2021 Did you look at my example? I placed a date time display between two labels. If you like I could place it right of a button. The answer is you can put your custom widget anywhere you want, just like a qt button or label or text edit. You widget inherits the ability to be placed in a window or a layout manager from QWidget. If you can do something to QWidget you can do the same thing to your custom widget. One thing really confuses me about your example. What do you think this does? class Window(QMainWindow, BuildDateTime): RE: pyqt5 layout - Nickd12 - Jan-13-2021 @deanhystad i was trying to figure out how to display it in the mainwindow class sorry im still trying to figure pyqt out and its hard to find information online with using python. Im actually messing around with the example you gave me right now but i need the window to be fullscreen im messing around with the idea of making a smart mirror interface. what im having a hard time doing is lets say i have a box that is 500x500 i place the date and time in that box. Date at 0x0 and time right under it. i want to take that 500x500 box with the content in it and display it on the main window and am i able to take that 500x500 box and move it using the coordinates instead of moving it central so lets say i want that box at 0x0 on main window or 100x100 and so on all i have to do is move the whole thing with coordinates and not each element individually RE: pyqt5 layout - deanhystad - Jan-18-2021 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_()) RE: pyqt5 layout - Axel_Erfurt - Jan-18-2021 (Jan-13-2021, 04:07 AM)Nickd12 Wrote: its hard to find information online with using python https://zetcode.com/gui/pyqt5/layout/ https://zetcode.com/gui/pyqt5/ |