Hi, I have my own widgets ( simple one) and want to put them in one file and call them from second file. This is like easygui but with pyqt4.
My problem - when I call my widget from second file nothing happens.

This is file with widgets (example).

import sys
from PyQt4 import QtGui, QtCore

class QuitButtonAA(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Icon AA')

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        self.connect(quit, QtCore.SIGNAL('clicked()'),
            QtGui.qApp, QtCore.SLOT('quit()'))

class QuitButtonBB(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Icon BB')

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        self.connect(quit, QtCore.SIGNAL('clicked()'),
            QtGui.qApp, QtCore.SLOT('quit()'))

And this is second file.

import mywidgets as g

# do this
g.QuitButtonBB

# then do this
g.QuitButtonAA

Could anyone help me?

TIA

Recommended Answers

All 3 Replies

A quick look says that classes/__init__() requires parens, i.e.

g.QuitButtonBB()

If that does not solve the problem, then post back. And "(code = python)" evidently does not work. Use the plain old "(code)" instead.

Take the spaces out of ""[code = python]"

What do you expect to happen with your test code?
If you test the module properly, it will work:

# create two separate windows with a button on it
# notice that the close button will close both windows

from PyQt4 import QtGui, QtCore

class QuitButtonAA(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(100, 200, 250, 150)
        self.setWindowTitle('Icon AA')

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        self.connect(quit, QtCore.SIGNAL('clicked()'),
            QtGui.qApp, QtCore.SLOT('quit()'))

class QuitButtonBB(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Icon BB')

        quit = QtGui.QPushButton('Close', self)
        quit.setGeometry(10, 10, 60, 35)

        self.connect(quit, QtCore.SIGNAL('clicked()'),
            QtGui.qApp, QtCore.SLOT('quit()'))


# test the potential module
if __name__ == '__main__':
    import sys
    app =  QtGui.QApplication(sys.argv)
    b1 = QuitButtonAA()
    b1.show()
    b2 = QuitButtonBB()
    b2.show()
    app.exec_()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.