Advertisement
MrPinzon

PyQT6_widgets_examples.py

Mar 30th, 2022
1,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.58 KB | None | 0 0
  1. #Examples shamelessly taken from
  2. #https://www.pythonguis.com/tutorials/pyqt6-widgets/
  3.  
  4. #Example 15. Lots of widgets visualized.
  5. """
  6. import sys
  7.  
  8. from PyQt6.QtCore import Qt
  9. from PyQt6.QtWidgets import (
  10.    QApplication,
  11.    QCheckBox,
  12.    QComboBox,
  13.    QDateEdit,
  14.    QDateTimeEdit,
  15.    QDial,
  16.    QDoubleSpinBox,
  17.    QFontComboBox,
  18.    QLabel,
  19.    QLCDNumber,
  20.    QLineEdit,
  21.    QMainWindow,
  22.    QProgressBar,
  23.    QPushButton,
  24.    QRadioButton,
  25.    QSlider,
  26.    QSpinBox,
  27.    QTimeEdit,
  28.    QVBoxLayout,
  29.    QWidget,
  30. )
  31.  
  32. # Subclass QMainWindow to customize your application's main window
  33. class MainWindow(QMainWindow):
  34.    def __init__(self):
  35.        super().__init__()
  36.  
  37.        self.setWindowTitle("Widgets App")
  38.  
  39.        layout = QVBoxLayout()
  40.        widgets = [
  41.            QCheckBox,
  42.            QComboBox,
  43.            QDateEdit,
  44.            QDateTimeEdit,
  45.            QDial,
  46.            QDoubleSpinBox,
  47.            QFontComboBox,
  48.            QLCDNumber,
  49.            QLabel,
  50.            QLineEdit,
  51.            QProgressBar,
  52.            QPushButton,
  53.            QRadioButton,
  54.            QSlider,
  55.            QSpinBox,
  56.            QTimeEdit,
  57.        ]
  58.  
  59.        for w in widgets:
  60.            layout.addWidget(w())
  61.  
  62.        widget = QWidget()
  63.        widget.setLayout(layout)
  64.  
  65.        # Set the central widget of the Window. Widget will expand
  66.        # to take up all the space in the window by default.
  67.        self.setCentralWidget(widget)
  68.  
  69. app = QApplication(sys.argv)
  70. window = MainWindow()
  71. window.show()
  72.  
  73. app.exec()
  74. """
  75.  
  76. #Example 16. QLabel are simple messages.
  77. """
  78. import sys
  79. from PyQt6.QtWidgets import (
  80.    QMainWindow, QApplication,
  81.    QLabel, QCheckBox, QComboBox, QListWidget, QLineEdit,
  82.    QLineEdit, QSpinBox, QDoubleSpinBox, QSlider
  83. )
  84. from PyQt6.QtCore import Qt
  85.  
  86. class MainWindow(QMainWindow):
  87.  
  88.    def __init__(self):
  89.        super(MainWindow, self).__init__()
  90.  
  91.        self.setWindowTitle("My App")
  92.  
  93.        widget = QLabel("Hello")
  94.        font = widget.font()
  95.        font.setPointSize(30)
  96.        widget.setFont(font)
  97.        widget.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
  98.  
  99.        self.setCentralWidget(widget)
  100.  
  101. app = QApplication(sys.argv)
  102. w = MainWindow()
  103. w.show()
  104. app.exec()
  105. """
  106.  
  107. #Example 17. Checkboxes.
  108. """
  109. import sys
  110. from PyQt6.QtWidgets import (
  111.    QMainWindow, QApplication,
  112.    QLabel, QCheckBox, QComboBox, QListWidget, QLineEdit,
  113.    QLineEdit, QSpinBox, QDoubleSpinBox, QSlider, QGraphicsPixmapItem
  114. )
  115. from PyQt6.QtCore import Qt
  116.  
  117. class MainWindow(QMainWindow):
  118.  
  119.    def __init__(self):
  120.        super(MainWindow, self).__init__()
  121.  
  122.        self.setWindowTitle("My App")
  123.  
  124.        widget = QCheckBox()
  125.        widget.setCheckState(Qt.CheckState.Checked)
  126.  
  127.        # For tristate: widget.setCheckState(Qt.PartiallyChecked)
  128.        # Or: widget.setTriState(True)
  129.        widget.stateChanged.connect(self.show_state)
  130.  
  131.        self.setCentralWidget(widget)
  132.  
  133.    def show_state(self, s):
  134.        print(s == Qt.CheckState.Checked)
  135.        print(s)
  136.  
  137. app = QApplication(sys.argv)
  138. w = MainWindow()
  139. w.show()
  140. app.exec()
  141. """
  142.  
  143. #Example 18. ComboBox.
  144. """
  145. import sys
  146. from PyQt6.QtWidgets import (
  147.    QMainWindow, QApplication,
  148.    QLabel, QCheckBox, QComboBox, QListWidget, QLineEdit,
  149.    QLineEdit, QSpinBox, QDoubleSpinBox, QSlider, QGraphicsPixmapItem
  150. )
  151. from PyQt6.QtCore import Qt
  152.  
  153. class MainWindow(QMainWindow):
  154.  
  155.    def __init__(self):
  156.        super(MainWindow, self).__init__()
  157.  
  158.        self.setWindowTitle("My App")
  159.  
  160.        widget = QListWidget()
  161.        widget.addItems(["One", "Two", "Three"])
  162.  
  163.        widget.currentItemChanged.connect(self.index_changed)
  164.        widget.currentTextChanged.connect(self.text_changed)
  165.  
  166.        self.setCentralWidget(widget)
  167.  
  168.    def index_changed(self, i): # Not an index, i is a QListItem
  169.        print(i.text())
  170.  
  171.    def text_changed(self, s): # s is a str
  172.        print(s)
  173.  
  174. app = QApplication(sys.argv)
  175. w = MainWindow()
  176. w.show()
  177. app.exec()
  178. """
  179.  
  180. #Example 19. LineEdit.
  181. """
  182. import sys
  183. from PyQt6.QtWidgets import (
  184.    QMainWindow, QApplication,
  185.    QLabel, QCheckBox, QComboBox, QListWidget, QLineEdit,
  186.    QLineEdit, QSpinBox, QDoubleSpinBox, QSlider, QGraphicsPixmapItem
  187. )
  188. from PyQt6.QtCore import Qt
  189.  
  190. class MainWindow(QMainWindow):
  191.  
  192.    def __init__(self):
  193.        super(MainWindow, self).__init__()
  194.  
  195.        self.setWindowTitle("My App")
  196.  
  197.        widget = QLineEdit()
  198.        widget.setMaxLength(10)
  199.        widget.setPlaceholderText("Enter your text")
  200.  
  201.        #widget.setReadOnly(True) # uncomment this to make readonly
  202.  
  203.        widget.returnPressed.connect(self.return_pressed)
  204.        widget.selectionChanged.connect(self.selection_changed)
  205.        widget.textChanged.connect(self.text_changed)
  206.        widget.textEdited.connect(self.text_edited)
  207.  
  208.        self.setCentralWidget(widget)
  209.  
  210.  
  211.    def return_pressed(self):
  212.        print("Return pressed!")
  213.        self.centralWidget().setText("BOOM!")
  214.  
  215.    def selection_changed(self):
  216.        print("Selection changed")
  217.        print(self.centralWidget().selectedText())
  218.  
  219.    def text_changed(self, s):
  220.        print("Text changed...")
  221.        print(s)
  222.  
  223.    def text_edited(self, s):
  224.        print("Text edited...")
  225.        print(s)
  226.  
  227. app = QApplication(sys.argv)
  228. w = MainWindow()
  229. w.show()
  230. app.exec()
  231. """
  232.  
  233. #Example 20. SpinBox.
  234. """
  235. import sys
  236. from PyQt6.QtWidgets import (
  237.    QMainWindow, QApplication,
  238.    QLabel, QCheckBox, QComboBox, QListWidget, QLineEdit,
  239.    QLineEdit, QSpinBox, QDoubleSpinBox, QSlider, QGraphicsPixmapItem
  240. )
  241. from PyQt6.QtCore import Qt
  242.  
  243. class MainWindow(QMainWindow):
  244.    def __init__(self):
  245.        super().__init__()
  246.  
  247.        self.setWindowTitle("My App")
  248.  
  249.        widget = QSpinBox()
  250.        # Or: widget = QDoubleSpinBox()
  251.  
  252.        widget.setMinimum(-10)
  253.        widget.setMaximum(3)
  254.        # Or: widget.setRange(-10,3)
  255.  
  256.        widget.setPrefix("$")
  257.        widget.setSuffix("c")
  258.        widget.setSingleStep(3)  # Or e.g. 0.5 for QDoubleSpinBox
  259.        widget.valueChanged.connect(self.value_changed)
  260.        widget.textChanged.connect(self.value_changed_str)
  261.  
  262.        self.setCentralWidget(widget)
  263.  
  264.    def value_changed(self, i):
  265.        print(i)
  266.  
  267.    def value_changed_str(self, s):
  268.        print(s)
  269.  
  270. app = QApplication(sys.argv)
  271. w = MainWindow()
  272. w.show()
  273. app.exec()
  274. """
  275.  
  276. #Example 21. Slider.
  277. """
  278. import sys
  279. from PyQt6.QtWidgets import (
  280.    QMainWindow, QApplication,
  281.    QLabel, QCheckBox, QComboBox, QListWidget, QLineEdit,
  282.    QLineEdit, QSpinBox, QDoubleSpinBox, QSlider, QGraphicsPixmapItem
  283. )
  284. from PyQt6.QtCore import Qt
  285.  
  286. class MainWindow(QMainWindow):
  287.    def __init__(self):
  288.        super().__init__()
  289.  
  290.        self.setWindowTitle("My App")
  291.  
  292.        widget = QSlider()
  293.  
  294.        widget.setMinimum(-10)
  295.        widget.setMaximum(3)
  296.        # Or: widget.setRange(-10,3)
  297.  
  298.        widget.setSingleStep(3)
  299.  
  300.        widget.valueChanged.connect(self.value_changed)
  301.        widget.sliderMoved.connect(self.slider_position)
  302.        widget.sliderPressed.connect(self.slider_pressed)
  303.        widget.sliderReleased.connect(self.slider_released)
  304.  
  305.        self.setCentralWidget(widget)
  306.  
  307.    def value_changed(self, i):
  308.        print(i)
  309.  
  310.    def slider_position(self, p):
  311.        print("position", p)
  312.  
  313.    def slider_pressed(self):
  314.        print("Pressed!")
  315.  
  316.    def slider_released(self):
  317.        print("Released")
  318.  
  319. app = QApplication(sys.argv)
  320. w = MainWindow()
  321. w.show()
  322. app.exec()
  323. """
  324.  
  325. #Example 22. Dial.
  326. """
  327. import sys
  328. from PyQt6.QtWidgets import (
  329.    QMainWindow, QApplication,
  330.    QLabel, QCheckBox, QComboBox, QListWidget, QLineEdit, QDial,
  331.    QLineEdit, QSpinBox, QDoubleSpinBox, QSlider, QGraphicsPixmapItem
  332. )
  333. from PyQt6.QtCore import Qt
  334.  
  335. class MainWindow(QMainWindow):
  336.    def __init__(self):
  337.        super().__init__()
  338.  
  339.        self.setWindowTitle("My App")
  340.  
  341.        widget = QDial()
  342.        widget.setRange(-10, 100)
  343.        widget.setSingleStep(0.5)
  344.  
  345.        widget.valueChanged.connect(self.value_changed)
  346.        widget.sliderMoved.connect(self.slider_position)
  347.        widget.sliderPressed.connect(self.slider_pressed)
  348.        widget.sliderReleased.connect(self.slider_released)
  349.  
  350.        self.setCentralWidget(widget)
  351.  
  352.    def value_changed(self, i):
  353.        print(i)
  354.  
  355.    def slider_position(self, p):
  356.        print("position", p)
  357.  
  358.    def slider_pressed(self):
  359.        print("Pressed!")
  360.  
  361.    def slider_released(self):
  362.        print("Released")
  363.  
  364. app = QApplication(sys.argv)
  365. w = MainWindow()
  366. w.show()
  367. app.exec()
  368. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement