Developing User Interfaces using PyQt – Lesson 12: Mozzarella Ashbadger – Navigation

Posted by


In this lecture, we will be exploring how to create navigation interfaces using PyQt. Navigation interfaces are essential for creating user-friendly applications that allow users to easily move between different screens or sections. In this tutorial, we will focus on creating a simple navigation interface using PyQt and demonstrate how to implement basic navigation functionalities such as switching between screens and passing data between screens.

To get started, make sure you have PyQt installed on your system. If you haven’t already installed PyQt, you can do so by running the following command in your terminal:

pip install PyQt5

Once you have PyQt installed, you can create a new PyQt application by creating a new Python script and importing the necessary modules:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget

Next, define a class for your main window that will serve as the container for your navigation interface. In the __init__ method of the class, create the necessary widgets and layout to set up the navigation interface:

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

        self.setWindowTitle("Navigation Demo")

        self.stack = []

        # Create buttons for navigating between screens
        self.button1 = QPushButton("Screen 1")
        self.button1.clicked.connect(self.show_screen1)

        self.button2 = QPushButton("Screen 2")
        self.button2.clicked.connect(self.show_screen2)

        self.button3 = QPushButton("Screen 3")
        self.button3.clicked.connect(self.show_screen3)

        # Create a layout to hold the navigation buttons
        layout = QVBoxLayout()
        layout.addWidget(self.button1)
        layout.addWidget(self.button2)
        layout.addWidget(self.button3)

        # Create a central widget to hold the layout
        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)

        self.show_screen1()

In the code above, we defined a NavigationWindow class that inherits from QMainWindow. We created three buttons that correspond to three different screens in our navigation interface – Screen 1, Screen 2, and Screen 3. Each button is connected to a respective function that will display the corresponding screen when clicked. We also created a stack attribute to keep track of the screens that the user has navigated to.

Next, define the functions for showing each screen:

    def show_screen1(self):
        self.stack.append("Screen 1")
        self.set_title()

    def show_screen2(self):
        self.stack.append("Screen 2")
        self.set_title()

    def show_screen3(self):
        self.stack.append("Screen 3")
        self.set_title()

In each of these functions, we simply add the name of the screen to the stack and call a function called set_title() to update the title of the application window:

    def set_title(self):
        title = "Navigation Demo - " + " > ".join(self.stack)
        self.setWindowTitle(title)

Now that we have set up the basic structure for our navigation interface, we can run the application using the following code:

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = NavigationWindow()
    window.show()
    sys.exit(app.exec_())

When you run the application, you should see a window with buttons for screens 1, 2, and 3. Clicking on each button will update the title of the window to reflect the current screen that the user has navigated to.

This is just a basic example of how to create a navigation interface using PyQt. You can expand on this by adding more screens, implementing more complex navigation logic, and customizing the appearance of your interface. PyQt provides a wide range of widgets and functionalities that you can use to create dynamic and user-friendly GUI applications. Experiment with different layouts, styles, and interactions to create a unique and engaging navigation interface for your application.

0 0 votes
Article Rating

Leave a Reply

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@wiouv
2 hours ago

Can you give a link to these icons?

@johnsaxondale21
2 hours ago

Why do so many programmers think switching from 'http' to 'https' makes things more secure? I don't know what it is about programmers but almost all of them never stop to question why adding an 's' makes it more secure. The reason 'http' is considered 'unsecure' is because it allows the 'http' website to see any information entered into a 'http' website, and because this might include credit card info, 'http' is therefore unsecure. The supposed solution for 'http' is 'https', and if you havent heard or simply dont know, the 's' stands for SECURE which means absolutely nothing in reality but the word 'secure' just feels soooo good to hear. Why would the government, global corporations, central bankers, etc that fund the organizations that create and implement the 'https' protocol ever lie to the general populace? The government, global corporations, and central bankers have shown time and time again that they are the absolute paragons of truth and honesty, why would they suddenly, out of the blue, decide to start deceiving us? It just doesn't make sense…

Obviously, Im being facetious, but in my experience, programmers are incomprehensibly naive. They get told to make something and they do it with a pep in their step and enthusiastic know it all attitude that exudes 'I know everything, I know all the facts about everything…' In reality the facts mean absolutely nothing, only knowing which facts matter more than other facts means something and the 's' in 'https' makes it so that when information is entered into a 'https' website, instead of the website seeing the information entered, the information is first sent to a list of certificate 'authorities' around the globe that approve of the connection for us retarded, dumb as dirt, worms. Its kinda like when your parents said they had to check your halloween candy for 'poison' before they let you eat the candy. Just like your parents, the certificate 'authorities' dont give a flying fuck about 'poison' they just want to take some free candy… In other words the certificate authorities get first dibs on your credit card info, and after the 'authorities' certify the information, the information is sent to the actual website. In other words the 's' in 'https' means your info is shared with most world governments, most global tech companies, and most central banking ngo fronts.

Dont believe me? If you got an android phone download the 'hidden settings' app and scroll down to 'certificate authorities' and look up who owns those companies. The chinese, japan, swiss, dont even attempt to hide it, it just straight up says 'Chinese government', or 'Swiss Government' or 'Japanese Government', isreali financial heads try to hide behind a bunch of shell companies, the cia tries to hide behind a bunch of shell companies, the british rothschild bank of england tries to hide behind a bunch of shell companies, but with a lil research one can piece together the connections. The 's' in 'https' does NOT mean secure it means SHARED. Hackers barely seem to get this, programmers got no clue and will argue with you for pointing it out as if you insulted their mother.

This same concept applies to the switch from telnet to ssh and ftp to sftp. The additional 'ss' does not mean doubly secured, but ask any programmer and they'll list every useless 'security protection' ssh provides that telnet doesnt. If you are sending shit over telnet or ftp and its important enough that you need to encrypt it, why in the flying fuck would you use ssh? The protocol was funded by the same people who want to steal your important info, the government, global corporations, and central banks have never once told the truth to the public majority, so why the fuck do programmers think ssh is more secure. Why not be a programmer and create your own unique encryption algorithm and use that to send messages over telnet? Its insane and im tired of it being tip-toed around. Apple recently 'discovered' that bug that allowed anyone who called any phone with a request to facetime to receive all data from the the phone dialed, ALL DATA. That means all things in memory, all pictures, all videos, all browsing history, all apps, all microphone input(all sound basically), fucking everything, simply because a person requested facetiming you. My programmer work collegues ate that story up like it was the word of god himself. Anyone who knows anything about software design architecture knows, that type of bug is not a bug its a feature, it would be 100% impossible to write a bug that allowed that type of behavior. Maybe its cause the codebase I work on is 30-35 years old and people are so used to horrible disgusting code that they dont realize massive app crashing bugs are not commonplace but holy shit is it gettin old.

@omarshehab3615
2 hours ago

q.scheme() don't work in my pc

@victornikolov537
2 hours ago

I realy like how you explain.
I am in love with Python and I want to know how to write a GUI apps.
I have tried to do what you show us here step by step just to see it working on my desktop
But in the last PyQt5 version there is no PyQt5.QtWebKitWidgets module
I've sow that it is deprecated since v.5.2 and replased with QtWebEngine
Wich is havier
I love simlicity but…
However
Can you show us how to do the same but using QtWebEngine
I hope I don't wrong the how the new module is called
Apologize my English

4
0
Would love your thoughts, please comment.x
()
x