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 8412573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:42:18+00:00 2026-06-10T00:42:18+00:00

I’ll start with the question and then try to explain: Is there a way

  • 0

I’ll start with the question and then try to explain:
Is there a way for an imported module to call a function in the module that imports it?

I am just learning to use Qt and am starting with Qt Designer to get some fundamentals worked out.

I have figured out how to create more than one “.ui” file in order to get the code for multiple windows and have managed to work out how to call the multiple windows from a main application by importing the code for the two windows.

For example, starting with win1.ui and win2.ui I create win1.py and win2.py – from my main application I import win1 and win2…

Note – I got this far by following this simple tutorial : http://www.youtube.com/watch?v=bHsC6WJsK-U&list=PLF4575388795F2531&index=10&feature=plpp_video

OK – now the question. If I have a button in win2, I know how to link that button to a function in the win2.py code. What I don’t know how to do is link the button in win2 to a function in my main application.

My only thought would be to add a function as an argument to the class that sets up the second window but if I do that then any changes to win2.ui will wreck the code that I have changed.

Thus, Is there a way for an imported module to call a function in the module that imports it?

I hope this is clear without adding a bunch of code that isn’t really relevant…

  • 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-06-10T00:42:20+00:00Added an answer on June 10, 2026 at 12:42 am

    Qt is based on event-driven programming. Generally when you start building up your widgets, what you are going to be wanting to do is providing information to receiver widgets via signals that are then processed. You don’t want to explicitly have a child widget know or require to call methods on a parent widget (this is not always the case, but it is good to avoid when possible).

    I’m gonna post some examples that don’t have UI files for ease here, but just assume you can build the same widget with designer and have it work the same way…

    testwidget.py

    from PyQt4 import QtGui, QtCore
    
    class TestWidget(QtGui.QWidget):
        textSaved = QtCore.pyqtSignal(str)
    
        def __init__( self, parent = None ):
            super(TestWidget, self).__init__(parent)
    
            # create the ui (or load it)
            self.__edit   = QtGui.QTextEdit(self)
            self.__button = QtGui.QPushButton(self)
            self.__button.setText('Save')
    
            layout = QtGui.QVBoxLayout()
            layout.addWidget(self.__edit)
            layout.addWidget(self.__button)
            self.setLayout(layout)
    
            # create connections
            self.__button.clicked.connect(self.emitTextSaved)
    
        def emitTextSaved( self ):
            # allow Qt's blocking of signals paradigm to control flow
            if ( not self.signalsBlocked() ):
                self.textSaved.emit(self.__edit.toPlainText())
    

    testwindow.py

    from PyQt4 import QtGui, QtCore
    import testwidget
    
    class TestWindow(QtGui.QMainWindow):
        def __init__( self, parent == None ):
            super(TestWindow, self).__init__(parent)
    
            # create the ui (or load it)
            self.__editor = testwidget.TestWidget(self)
            self.setCentralWidget(self.__editor)
    
            # create connections
            self.__editor.textSaved.connect(self.showMessage)
    
        def showMessage( self, message ):
            QtGui.QMessageBox.information(self, 'Message', message)
    

    So, here you can see that instead of thinking about it like – “when I click the button in TestWidget, I want to show a message in TestWindow” and explicitly link the two methods, you expose a signal that the TestWidget will emit out when the user performs an action, then connect that signal to the showMessage slot of the TestWindow. This way, your smaller widgets become more independent, and its more a matter of how you connect to each event that drives your application.

    I could have done something like self.parent().showMessage(self.__edit.toPlainText()) within the TestWidget’s emitTextSaved method to call the method directly – but this is not a good design.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Specifically, suppose I start with the string string =hello \'i am \' me And
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.