1
\$\begingroup\$

I have started learning PyQt5 and wanted to create a simple window to test my skills. So I have created this window and I want to receive reviews on any aspect of the code.

from PyQt5.QtWidgets import QMainWindow, QApplication 
from PyQt5.QtWidgets import QVBoxLayout,QSplitter, QFormLayout, QLabel, QFrame, QPushButton
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
import sys
class Window(QMainWindow):
 def __init__(self):
 super().__init__()
 self.create_menu_bar()
 self.vbox = QVBoxLayout()
 self.create_body()
 self.show()
 def create_menu_bar(self):
 title = 'First Porgram'
 self.setWindowTitle(title)
 self.setGeometry(300, 150, 500, 300)
 self.setStyleSheet('background-color:#333;color:#ccc')
 self.setFont(QFont('Serif', 10))
 self.menu_bar = self.menuBar()
 self.file_menu = self.menu_bar.addMenu("File")
 self.file_menu.addAction('New')
 self.file_menu.addAction('Open')
 self.file_menu.addAction('Save')
 self.file_menu.addAction('Save as')
 self.file_menu.addAction('Exit') 
 self.view_menu = self.menu_bar.addMenu("View")
 self.view_menu.addAction('set Full Screen')
 self.view_menu.addAction('show Status Bar') 
 self.edit_menu = self.menu_bar.addMenu("Edit")
 self.edit_menu.addAction('Cut')
 self.edit_menu.addAction('Copy')
 self.edit_menu.addAction('Paste')
 self.edit_menu.addAction('Find')
 self.edit_menu.addAction('Replace') 
 self.help_menu = self.menu_bar.addMenu("Help")
 self.help_menu.addAction('Help')
 self.help_menu.addAction('About')
 def create_body(self):
 form_frame = QFrame()
 form_frame.setFrameShape(QFrame.StyledPanel)
 form_frame.setMinimumWidth(150)
 form_lay = QFormLayout()
 f_label = QLabel('Welcome')
 s_label = QLabel('Installation')
 p_push = QPushButton('Sign in')
 p_push.setContentsMargins(10, 20, 10, 10)
 self.vbox = QVBoxLayout()
 form_lay.addRow(f_label)
 form_lay.addRow(s_label)
 form_lay.addRow(p_push)
 form_frame.setLayout(form_lay)
 ver_frame = QFrame()
 ver_frame.setFrameShape(QFrame.StyledPanel)
 ver_box = QVBoxLayout()
 ver_box.setContentsMargins(25, 20, 25, 25)
 intro_label = QLabel("Welcome to The Open Space ")
 intro_label.setFont(QFont('Serif', 16))
 ver_box.addWidget(intro_label)
 ver_frame.setLayout(ver_box)
 splitter = QSplitter(Qt.Horizontal)
 splitter.addWidget(form_frame)
 splitter.addWidget(ver_frame)
 self.vbox.addWidget(splitter)
 self.setCentralWidget(splitter)
 def contextMenuEvent(self, event):
 men = QMenu()
 men.addAction('New')
 men.addAction('Open')
 quit = men.addAction('Quit')
 action = men.exec_(self.mapToGlobal(event.pos()))
 if action is quit:
 self.close()
def main():
 app = QApplication(sys.argv)
 win = Window()
 app.exec_()
if __name__ == '__main__':
 main()

here is a screenshot of my window enter image description here

Also, how can I add to a top margin on my sign in button, and is there any way I can inherit my context menu's color from my class?

AlexV
7,3532 gold badges24 silver badges47 bronze badges
asked May 11, 2019 at 16:45
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Qt Style Sheets Reference https://doc.qt.io/qt-5/stylesheet-reference.html

Qt Style Sheets support various properties, pseudo-states, .and subcontrols that make it possible to customize the look of widgets.

Qt Style Sheets Examples https://doc.qt.io/archives/qt-4.8/stylesheet-examples.html

Try it:

import sys
from PyQt5.QtWidgets import (QMainWindow, QApplication, QVBoxLayout, QSplitter, 
 QFormLayout, QLabel, QFrame, QPushButton, 
 QMenu, QAction) # + 
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QIcon
class Window(QMainWindow):
 def __init__(self):
 super().__init__()
 self.create_menu_bar()
# self.vbox = QVBoxLayout() # -
 self.create_body()
 def create_menu_bar(self):
 self.menu_bar = self.menuBar()
 self.file_menu = self.menu_bar.addMenu("File")
 self.file_menu.addAction('New')
 self.file_menu.addAction('Open')
 self.file_menu.addAction('Save')
 self.file_menu.addAction('Save as')
# self.file_menu.addAction('Exit') 
 # +++
 self.exit_menu = QAction(QIcon("D:/_Qt/img/exit.png"),'&Exit', self)
 self.exit_menu.setShortcut('Ctrl+Q')
 self.exit_menu.triggered.connect(self.close)
 self.file_menu.addAction(self.exit_menu) 
 self.view_menu = self.menu_bar.addMenu("View")
 self.view_menu.addAction('set Full Screen')
 self.view_menu.addAction('show Status Bar') 
 self.edit_menu = self.menu_bar.addMenu("Edit")
 self.edit_menu.addAction('Cut')
 self.edit_menu.addAction('Copy')
 self.edit_menu.addAction('Paste')
 self.edit_menu.addAction('Find')
 self.edit_menu.addAction('Replace') 
 self.help_menu = self.menu_bar.addMenu("Help")
 self.help_menu.addAction('Help')
 self.help_menu.addAction('About')
 def create_body(self):
 form_frame = QFrame()
 form_frame.setFrameShape(QFrame.StyledPanel)
 form_frame.setMinimumWidth(150)
 f_label = QLabel('Welcome')
 s_label = QLabel('Installation')
 p_push = QPushButton('Sign in')
 p_push.setContentsMargins(10, 20, 10, 10)
 form_lay = QFormLayout()
 form_lay.addRow(f_label)
 form_lay.addRow(s_label)
 form_lay.addRow(p_push)
 form_frame.setLayout(form_lay)
 ver_frame = QFrame()
 ver_frame.setFrameShape(QFrame.StyledPanel)
 intro_label = QLabel("Welcome to The Open Space ")
 intro_label.setFont(QFont('Serif', 16))
 ver_box = QVBoxLayout()
 ver_box.setContentsMargins(25, 20, 25, 25) 
 ver_box.addWidget(intro_label)
 ver_frame.setLayout(ver_box)
 splitter = QSplitter(Qt.Horizontal)
 splitter.addWidget(form_frame)
 splitter.addWidget(ver_frame)
 self.vbox = QVBoxLayout()
 self.vbox.addWidget(splitter)
 self.setCentralWidget(splitter)
 def contextMenuEvent(self, event):
 men = QMenu()
 men.addAction('New')
 men.addAction('Open')
 quit = men.addAction('Quit')
 action = men.exec_(self.mapToGlobal(event.pos()))
 if action is quit:
 self.close()
StyleSheet = '''
QMainWindow {
 background-color: #333;
 color: red;
}
/* QMenuBar --------------------------------------------------------------- */
QMenuBar {
 background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
 stop:0 lightgray, stop:1 darkgray);
}
QMenuBar::item {
 spacing: 3px; 
 padding: 2px 10px;
 background-color: rgb(210,105,30);
 color: rgb(255,255,255); 
 border-radius: 5px;
}
QMenuBar::item:selected { 
 background-color: rgb(244,164,96);
}
QMenuBar::item:pressed {
 background: rgb(128,0,0);
}
/* QMenu ------------------------------------------------------------------ */
QMenu {
 font: 12pt;
 background-color: white;
 color: black;
}
QMenu::item:selected {
 color: gray;
}
/* QSplitter -------------------------------------------------------------- */
QSplitter::handle:horizontal {
 width: 2px;
 background-color : green;
}
QSplitter::handle:vertical {
 height: 2px;
 background-color : green;
}
/* ------------------------------------------------------------------------ */
QLabel {
/* background-color : blue;*/
 color: #ccc;
}
QPushButton {
 min-width: 36px;
 min-height: 36px;
 border-radius: 7px;
 background: #777;
}
QPushButton:hover {
 color: white;
 background: #999;
}
QPushButton:pressed {
 background-color: #bbdefb;
 color: green;
}
'''
if __name__ == '__main__':
 app = QApplication(sys.argv)
 app.setStyleSheet(StyleSheet) # <---
 win = Window()
 win.setWindowTitle('First Porgram')
 win.setWindowIcon(QIcon("D:/_Qt/img/qt-logo.png")) 
 win.setGeometry(300, 150, 500, 300)
 win.show()
 app.exec_()

enter image description here

answered May 19, 2019 at 14:07
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Review. Good answers that get upvoted contain observations about the Original Posters code. Good answers may not contain any code at all. Could you please comment on the users code as well. Please take a look at our guidelines for a good answer at codereview.stackexchange.com/help/how-to-answer. \$\endgroup\$ Commented May 19, 2019 at 14:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.