Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:57:23+00:00 2026-05-30T21:57:23+00:00

I have a main dialog and on that dialog there is a button. When

  • 0

I have a main dialog and on that dialog there is a button. When the button is clicked, I want to open an another dialog.

Main Dialog code (Function which is called when the button is clicked in the main dialog):

def add_host(self):
        x=add_host.Ui_Dialog1()
        x.main()

default function:

if __name__ == "__main__":
    import sys
    global app
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

Secondary dialog (add_host.py) code snapshot:

def main(self):
        app1 = QtGui.QApplication(sys.argv)
        Dialog1 = QtGui.QDialog()
        ui1 = Ui_Dialog1()
        ui1.setupUi1(Dialog1)
        Dialog1.show()
        sys.exit(app.exec_())

So when I run the code, it opens the secondary dialog but when I close it, it just freezes, and I get this error message:

 File "testbot.py", line 175, in add_host
    x.main()
  File "/home/ppp/ppp/add_host.py", line 74, in main
    sys.exit(app.exec_())
NameError: global name 'app' is not defined

Which does make sense, but I have no idea how to resolve it. I try several combinations without success, including adding and deleting app.exec_().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:57:26+00:00Added an answer on May 30, 2026 at 9:57 pm

    You cannot create multiple QApplications inside of the same script and thread. You are only supposed to have one…

    This should be more like:

    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        Dialog = QtGui.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    

    No global app. Although you should be doing your setupUI from within the class of your dialog.

    Then when you want to show another dialog from with your app…say main(), you just create it and call show()

    Here is a really basic example:

    class Dialog(QDialog)
        def __init__(self, parent):
            super(Dialog, self).__init__(parent)
            self.otherDialog = QDialog(parent=self)
    
            self.otherDialog.show()
    
    
    if __name__ == "__main__":
        app = QApplication([])
        dialog = Dialog()
        dialog.show()
        app.exec_()
    

    You create a single QApplication and start its event loop by calling exec_(). From that point on, your main application is free to create more QWidgets. You never create another QApplication again at this point.

    Also, I dont understand this part of your code:

    def add_host(self):
        x=add_host.Ui_Dialog1()
        x.main()
    

    The fact that you are calling a main() method on your UI object makes me think you are modifying the UI file and adding functionality to it, which you should not be doing. That ui file is subject to be overwritten every time you make changes in QT Designer and save out a new one. You should only be importing it and using its setupUI() method to apply it to your own custom classes.

    A note about organization of your modules

    When you are designing a PyQT application, you will always have a single entry point that will be called to start your app. This is the single and only location that should be creating your QApp and starting the event loop, and is usually done with in a if __name__ == "__main__" block to ensure its only done when its the main script. It should not be done within methods of your objects. For all your other modules where you define other Dialog, Widgets, etc, these should simply be classes that you import. As long as you have a running QApp, you are free to create and show these widgets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
i have a table called category in which i have main category ids and
I have a main window (#1) on my webpage from which I open a
I have a modelless dialog that creates a thread, and if the cancel button
I have a main window in Which I have a button. When i click
I want to make it so that there is a ListView with a Button
On our main data entry screen, we have an OK/Cancel dialog in the OnBeforePost
I have main categories and sub categories. I want to be able to sort
I have a main asp.net app, which is written in asp.net 1.1. Runnning underneath
I have a custom Dialog that shows a few buttons. The idea is to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.