Design of window with PyQt 5
-
Hello dear users iam using PyQt5 and i noticed all my programs looks old. They have old looking design like this
But how to make mine programs looks like this one
Thank you a lot for your help !
-
Hi,
What version of PyQt5 are you using ?
What version of Windows are you running ?
How are you starting your application ?
Can you share a minimal reproducible example that shows this ? -
class Window(QMainWindow):
def init(self):
super().init()self.setGeometry(300, 300, 400, 500) self.setWindowTitle("Please login")
this is how i start program.
iam using windows 10 and PyQt5 5.14.12 -
Hi,
No this is not. This is just a class declaration.
It would be best to provide all the code that allows to start you application properly. -
Like this iam sorry
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow
import sys
from PyQt5.QtWidgets import QApplicationclass Window(QMainWindow):
def __init__(self): super().__init__() self.name = "" self.password = "" self.setGeometry(300, 300, 400, 500) self.setWindowTitle("Hello") self.b1 = QtWidgets.QPushButton(self) self.b1.setText("Button") self.b1.move(120, 155) self.b1.resize(170, 32) self.show()
if name == "main":
app = QApplication(sys.argv)
window = Window()
sys.exit(app.exec()) -
Don`t do these setGeometry!
class Window(QMainWindow): def __init__(self, parent=None): super(Window, self).__init__(parent) self.setWindowTitle("Please login") if __name__ == "__main__": ... app_window = Window() app_window.show() desktop = QtWidgets.QApplication.desktop() resolution = desktop.availableGeometry() app_window.setFixedWidth(resolution.width() / 2) app_window.setFixedHeight(resolution.height() / 2) app_window.move(resolution.center() - app_window.rect().center()) sys.exit(app.exec_())
-
Or if want to save stretch:
class Window(QMainWindow): def __init__(self, parent=None): super(Window, self).__init__(parent) self.setWindowTitle("Please login") self.setMinimumWidth(resolution.width() / 2) self.setMinimumHeight(resolution.height() / 2) ... if __name__ == "__main__": ... app = QtWidgets.QApplication(sys.argv) desktop = QtWidgets.QApplication.desktop() resolution = desktop.availableGeometry() app_window = Window() app_window.show() app_window.move(resolution.center() - app_window.rect().center()) sys.exit(app.exec_())
And button to the layout )
-
@Volodymyr14 Thank you for tip !!!
-
@Samuel-Bachorik no problem. The second variant is better because the screens are different and the size will be from minimum signed to a resolution of the screen. Really, in applications, it is not a good idea to use fixed sizes, set geometry, fixed resize and move. No fixed integers with sizes, just relative values. For example, to the fixed size put the size of sceen or widget divided on 2.
-
@Samuel-Bachorik
All thisself.b1.move(120, 155)
-type stuff is possible but is usually not the right way to go about things. You would normally createQLayout
s on your widgets --- if you are really using aQMainWindow
for your app that would be on theQMainWindow::centralWidget()
--- and then add your child widgets onto those layouts. Not doing so may lead to unexpected sizing/positioning issues. If you have not read through https://doc.qt.io/qt-5/layout.html, may I suggest you do so and reconsider in that light. -
Thank you for you help guys ! But iam not sure exatly about these sizes of my window. When i was working GUI in Tkinter whole program was able to scale without doing something extra. But at least how can i stop to scaling my window ? Just permament ressolution and no resizing allowed. You know also when i do not define set geometry from where this program know how big my window should be ?
-
I need to learn and figure it out how to make this in right way. What should i do when i want scale with all widgeds in window when i try to resize it while using.
-
Thank you guys for your help !
-
@Samuel-Bachorik You don`t need to care about scaling. If you will use a resolution of the desktop you will have the window that will be similar for any device with any screen resolution. To build the elements inside this window you can build these elements (buttons, fields, etc) with layouts without setting sizes explicitly. It is better to use the QGridLayout, or you can use QVBoxLayout or QHBoxLayout if the app is simple in functional elements. Or another way to make sizes relative to the window. For example:
self.button.setFixedWidth(self.width() / 3)
That will be button width as one-third of the window size, and it will work for any device with any resolution. And do not any these "resize(100, 100)".