[PyQt] Can I create more than one instance of a class?

Sundance sundance at ierne.eu.org
Wed Jun 18 09:20:16 BST 2008

Hi Adonay,

> Hi, I'm programing a little app which needs to open more than one
> time, the same window.
> Now I make an import and instance the window like this.
> from new_window import *

Bad idea. As a rule of thumb, you don't want to use 'import *' when you 
can avoid it. It's okay while your app is small; but once it grows, 
trust me, you'll be glad you can tell at a glance what comes from what 
module. So you may want to learn the best practice right away and 
avoid 'import *'. :)

> a = new_window
> a.show()
> b = new_window
> b.show()
> c = new_window
> c.show()

You are referencing the SAME instance, 'new_window', in all cases! If 
you want different instances, you need to instantiate your class each 
time. So, assuming your class is called MyWindow, your code should look 

from MyWindow import MyWindow ## Give the module a descriptive name too!

a = MyWindow()
b = MyWindow()
c = MyWindow()

print a, b, c

Perhaps your are not very well at ease with how Python works yet? If so, 
there are excellent tutorials out there:
- http://docs.python.org/tut/
- http://www.diveintopython.org/toc/


-- S.

More information about the PyQt mailing list