Tuesday, March 29, 2022

Button with PyQt6

This sample program demonstrate how to format a button, place it on a specific location in the window and try to execute a command upon pressing.

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
import sys
import os
from PyQt6.QtWidgets import QApplication,  QWidget, QPushButton

class Window(QWidget):

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

        self.initUI()

    def initUI(self):

        pb1 = QPushButton('Exit', self)
        pb1.setGeometry(50, 20, 250, 30)
        pb1.setStyleSheet('QPushButton {background-color: #2F569B; color: #d4d4d4;}')
        pb1.clicked.connect(onClick_pb1)
        
        pb2 = QPushButton('Click Me!', self)
        pb2.setGeometry(50, 60, 250, 30)
        pb2.setStyleSheet('QPushButton {background-color: #2F569B; color: #d4d4d4;}')
        pb2.clicked.connect(self.onClick_pb2)
        
        pb3 = QPushButton('Open Notepad', self)
        pb3.setGeometry(50, 100, 250, 30)
        pb3.setStyleSheet('QPushButton {background-color: #2F569B; color: #d4d4d4;}')
        pb3.clicked.connect(onClick_pb3)

        self.setGeometry(25, 45, 350, 150)
        self.setWindowTitle('Post 1')
        self.show()
        
 
    def onClick_pb2(self):
 
       print("clicked")
       
 
        
def onClick_pb3():
    os.popen(r"C:\WINDOWS\system32\notepad.exe")


    
def onClick_pb1():
    exit()

def main():

    app = QApplication(sys.argv)
    ex = Window()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

No comments:

Post a Comment