Wednesday, April 27, 2022

Create Volume Control Dial, List box, Toggle Button, Slider and a Spin Box in PyQt6

This demo program shows how to create Volume Control Dial, List box, Toggle Button, Slider and a Spin Box and place on specific location in a window.


The output:



The code:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from PyQt6.QtWidgets import (QWidget, QSlider, QListWidget, QSpinBox, QPushButton, QDial,
        QLabel, QApplication)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap
import sys

on_load = 0
class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):
        self.redb = QPushButton('Horizontal', self)
        self.redb.setCheckable(True)
        self.redb.setGeometry(20, 5, 80, 20)
        self.redb.clicked[bool].connect(self.setColor)
        
        self.sld = QSlider(Qt.Orientation.Horizontal, self)
        self.sld.setFocusPolicy(Qt.FocusPolicy.NoFocus)
        self.sld.setGeometry(30, 45, 200, 30)
        self.sld.valueChanged[int].connect(self.changeValue)
        self.sld.setSingleStep(2)
        
        self.sld1 = QSlider(Qt.Orientation.Vertical, self)
        self.sld1.setFocusPolicy(Qt.FocusPolicy.NoFocus)
        self.sld1.setGeometry(255, 45, 30, 200)
        self.sld1.valueChanged[int].connect(self.changeValue1)
        
        self.sld1.setSingleStep(2)
        
        self.label = QLabel(self)       
        self.label.setGeometry(30, 20, 200, 30)
        self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        
        self.widget = QListWidget(self)
        self.widget.addItems(["One", "Two", "Three"])
        self.widget.setGeometry(30, 95, 75, 120)
        self.llabel = QLabel(self)       
        self.llabel.setGeometry(100, 115, 75, 30)
        self.llabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.label.setText('slide the bar')
        self.llabel.setText('click an item')
        #self.widget.currentItemChanged.connect(self.index_changed)
        self.widget.currentTextChanged.connect(self.text_changed)
        
        self.widget1 = QSpinBox(self)
        self.widget1.setMinimum(-10)
        self.widget1.setMaximum(10)
        self.widget1.setPrefix("$")
        self.widget1.setSuffix("c")
        self.widget1.setGeometry(30, 235, 75, 30)
        self.slabel = QLabel(self)       
        self.slabel.setGeometry(115, 235, 75, 30)
        self.slabel.setText('click to change the value')
        
        self.dialer = QDial(self)
        self.dialer.setRange(0, 100)
        self.dialer.setSingleStep(0.5)
        self.dialer.valueChanged.connect(self.value_changed_dialer)
        self.dialer.setNotchesVisible(True)
        self.dialer.move(110,300)
        self.dslabel = QLabel(self)       
        self.dslabel.setGeometry(125, 400, 75, 30)
        self.dslabel.setText('rotate the dial')
        self.dslabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        
        self.widget1.textChanged.connect(self.value_changed_str)
        self.setGeometry(200, 100, 350, 430)
        self.setWindowTitle('Post 27')
        self.show()
    def value_changed_dialer(self, i):
        self.dslabel.setText(str(i))
        
    def setColor(self, pressed):
        if pressed:
            self.redb.setText("Vertical")
            
        else:
            self.redb.setText("Horizontal")
            
    
        
    def value_changed_str(self, s):
        self.slabel.setText(s)
    #def index_changed(self, i): # Not an index, i is a QListItem
    #    print(i.text())
    #    self.llabel.setText(i.text())
    def text_changed(self, s): # s is a str
        global on_load
        print(s)
        if on_load == 1:
          self.llabel.setText(s)
        on_load = 1
    def changeValue(self, value):

        self.label.setText(str(value))
        self.sld1.setValue(value)
        self.sld1.repaint()
    def changeValue1(self, value):

        self.label.setText(str(value))
        self.sld.setValue(value)
        self.sld.repaint()        

def main():
    
    app = QApplication(sys.argv)
    ex = Example()
    
    sys.exit(app.exec())
   

if __name__ == '__main__':
    main()
    on_load = 1

No comments:

Post a Comment