Thursday, April 14, 2022

Calling a Window upon pressing of a Button in PyQt6

This demo program shows how to call window upon pressing a button, add tool tip to button and format the tool tip text using html.

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
import sys
from PyQt6 import QtGui
from PyQt6.QtWidgets import QApplication, QMainWindow, QPushButton, QToolTip, QMessageBox, QLabel

class Window2(QMainWindow):                           # <===
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Welcome!!!")
        self.top = 200
        self.left = 200
        self.width = 300
        self.height = 350
        self.setGeometry(self.top, self.left, self.width, self.height)
        self.pushButton = QPushButton("Logout", self)
        self.pushButton.move(100, 75)
        self.pushButton.setToolTip("<h4>End the Session</h4>")
        self.pushButton.clicked.connect(self.window3) 
    def window3(self):
             # <===
        self.w = Window()
        self.w.show()
        self.hide()       
class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.title = "Login Window"
        self.top = 100
        self.left = 100
        self.width = 300
        self.height = 200

        self.pushButton = QPushButton("Login", self)
        self.pushButton.move(100, 75)
        self.pushButton.setToolTip("<h4>Start the Session</h4>")

        self.pushButton.clicked.connect(self.window2)             

        self.main_window()

    def main_window(self):
        self.label = QLabel("The Cute App", self)
        self.label.move(100, 50)
        self.setWindowTitle(self.title)
        self.setFixedSize(self.width, self.height)
        self.move(self.top, self.left)
        
        self.show()

    def window2(self):                                             
        self.w = Window2()
       
        self.w.show()
        self.hide()
 
            

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec())

No comments:

Post a Comment