0% found this document useful (0 votes)
14 views

Python PyQt6 Notes

The document provides a comprehensive guide on using PyQt6 for creating GUI applications, detailing the process of setting up windows, layouts, and various widgets. It includes essential imports, widget creation, layout management, and event handling, as well as specific commands for customizing widgets like buttons, text entries, and message boxes. Additionally, it emphasizes the importance of executing specific commands to display the window and manage user interactions effectively.

Uploaded by

nutmeg278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python PyQt6 Notes

The document provides a comprehensive guide on using PyQt6 for creating GUI applications, detailing the process of setting up windows, layouts, and various widgets. It includes essential imports, widget creation, layout management, and event handling, as well as specific commands for customizing widgets like buttons, text entries, and message boxes. Additionally, it emphasizes the importance of executing specific commands to display the window and manage user interactions effectively.

Uploaded by

nutmeg278
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

PyQt6:

IMPORTANT THINGS TO DO
Create window then widgets
To make more than one widget(all the time) make one large widget that stores the
layout and then display is at "setCentralWidget"
The order of when widgets are made influences their position on the layout

IMPORTS:
Read Images -> icons
from PyQt6.QtGui import QIcon
Creates Widgets
from PyQt6.QtGui import QLabel
Align Text or Grid Layouts:
from PyQt6.QtCore import Qt
Read Images to widgets:
from PyQt6.QtGui import QPixmap
Push Buttons:
from PyQt6.QtWidgets import QPushButton
VBoxLayout(the exact same for Hboxlayout):
from PyQt6.QtWidgets import QVBoxLayout
More complex widgets:
from PyQt6.QtWidgets import QWidget
QGridLayout:
from PyQt6.QtWidgets import QGridLayout
QLineEdit lets you make a text entry box:
from PyQt6.QtWidgets import QLineEdit
Drop down boxes:
from PyQt6.QtWidgets import QComboBox
Spin Boxes:
from PyQt6.QtWidgets import QSpinBox
Double Spinboxes:
from PyQt6.QtWidgets import QDoubleSpinBox
Add a Radio Button (Multip choice):
fomr PyQt6.QtWidgets import QRadioButton
Makes it easier to work with buttons especially Radio:
from PyQt6.QtWidgets import QButtonGroup
Pop up message:
from PyQt6.QtWidgets import QMessageBox
Msg that can have an imput:
from PyQt6.QtWidgets import QInputBox
Color Picker msgbox:
from PyQt6.QtWidgets import QColorDialog
Font Picker:
from PyQt6.QtWidgets import QFontDialog

Design Process:
Make Imports
Set Up Window
Set Layout
Add Widgets

The window will not show untill these commands are put in:
window.show()
app.exec()

Maximized Window:
window.showMaximized()
Minimized Window:
window.showminimized()

Set icons:
window.setWindowIcon(QIcon("PlaceHolderAppIcon.png"))

Create a widget:
label = QLabel("Text Goes Here")

Set a widget to Center position:


window.setCentralWidget(label)

Alight a Widget:
label = QLabel("Text Goes Here", alignment=Qt.AlignmentFlag.Align_______)

Add a font customization for a specific text(font):


font = window.font()

Set font to bold(after creating Qlabel):


font.setBold(True)

Set font size (after creating QLabel):


font.setPointSize(19)

Use font created in a widget:


label.setFont(font)

Make a widget an image:


label = QLabel()
label.setPixmap(QPixmap("PlaceHolderAppIcon.png"))

Scale entire image:


label.setPixmap(QPixmap("PlaceHolderAppIcon.png").scaled(250,250))

Scale Only Height of image:


label.setPixmap(QPixmap("PlaceHolderAppIcon.png").scaledToHeight(250))

Scale Only Width of image:


label.setPixmap(QPixmap("PlaceHolderAppIcon.png").scaledToWidth(250))

Create a button with text:


button = QPushButton("Click Me")

Set Size of a button(multiple other lines):


button.setFixedSize(200, 200)

Make a button use a font setting:


button.setFont(font)

Create a centeral complex widget:


centerwidget = QWidget()

Create instance of a layout applied to a widget:


centerwidget.setLayout(layout)

Add Widget to a Layout:


layout.addWidget()

Add Layout to a Parent Layout:


parentlayout.addLayout()

Apply Spacing(To the widget just created):


buttonLayout.addSpacing(50)

Add a widget to QGridLayout:


parentLayout.addWidget(label,0, 0)
name of widget,x, y

Align a widget to a QTGrid:


label = QLabel("This is a Label", alignment = Qt.AlignmentFlag.AlignCenter)

Make a widget take up more space on a grid pattern


parentLayout.addWidget(label, 0, 0, RowWidth, ColomWidth)

Set Horizonal Spacing on a Grid Layout:


parentLayout.setHorizontalSpacing(500)

Set Row or Colum minimum height(row applied, spacing amount):


parentLayout.setRowMinimumHeight(1, 200)

Create a QLineEdit box:


textbox = QLineEdit()

Make a box enter text in dots(password):


textbox.setEchoMode(QLineEdit.EchoMode.Password)

Create a class:
class Window(QMainWindow):

Check for if a button is clicked in a class:


self.button.clicked.connect(self.clickhandler)

Do something when button is clicked:


def clickhandler(self):
print("button clicked")

Print the text of a text entry (textbox):


print(self.textbox.text())

Set Max Characters on a text box:


textbox.setMaxLength(10)

Add item to a drop down:


self.combobox.addItem("")

Check Current Working Directory(needs os imported):


print("Current Working Directory:", os.getcwd())

Print the output of a drop down:


print(combobox.currentText())

Create a maximum or minimum for a spinbox:


spinbox.setMaximum(10)
spinbox.setMinimum(2)

print value of a spinbox:


print(spinbox.value())
step of a spinbox:
spinbox.setSingleStep(2)

Add radio button to a button group:


self.buttongroup.addButton(self.multchoiceA)

Check for a cliked button in a button group:


print(buttongroup.checkedButton())

Get the text of a clicked button(only for radio buttons):


print(buttongroup.checkedButton().text())

Convert a Qt Desinger .ui to .py: (cmd)


pyuic6 QtDesignerTest.ui -o design.py

Exc a message box:


msgbox.exec()

Set the text of a msgbox:


msgbox.setText("TextHere")

Set Title of a msgbox:


msgbx.setWindowTitle("TITLE")

Add icon (preset) to a msgbox:


msgbox.setIcon(QMessageBox.Icon.Question)

set standard button of a msgbox:


msgbox.setStandardButtons(QMessageBox.StandardButton.ok)

Add More standard buttons to a msgbox:


msgbox.setStandardButtons(QMessageBox.StandardButton.Ok |
QMessageBox.StandardButton.No)

Store answer of a msgbox:


clickedButton = msgbox.exec()

Check what what click was stored:


if clickedButton == QMessageBox.StandardButton.Ok:

Create a inputbox:
msg = QInputDialog()

Print the output of a inputbox:


print(msg.textValue())

Set Label of an inputbox:


msg.setLabelText("Enter You Race")

Store clicked button of an inputbox:


clickedButton = msg.exec()

Change the InputMode of a inputbox to integers:


msg.setInputMode(QInputDialog.InputMode.IntInput)

Print the Int value of an inputbox:


print(msg.intValue())

Make a msgbox a color picker:


msg = QColorDialog()

Print current color selected:


print(msg.currentColor())

Print Hexcode of current color selected:


print(msg.currentColor().name())

Create a font picker msgbox:


msg = QFontDialog()

Print the current font:


print(msg.currentFont())

Set a label to the chosen font of a msgbox:


self.label.setFont(msg.currentFont())

Print the current font of a msgbox (readable):


print(msg.currentFont().family())

Create a class to hold the main window and gui:


class Window(QMainWindow):
def __init__(self):
super().__init__()

VERY IMPORTANT to trasfer a .py file from Qt designer:


from filenamehere import Ui_MainWindow

VERY IMPORTANT to actully make your Qt GUI work:


ui = Ui_MainWindow()
ui.setupUi(window)

Refer to the current text in a line edit widget:


self.ui.lineEdit.text()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy