Split pyqt project in many files

Hi all, I would like to write a pyqt project splitting it in many py files. for example I would define slot functions in another file to create a readable project. This is possible? has someone some tutorials and examples?
many thanks in advance

Hello d,

of course you can split Python projects into many py files. That is
standard practice.

You just put parts of the code into different files, and then import
them as needed. You should use a package structure, and absolute imports
are easier than relative imports.

There are many Python tutorials on the internet, have you done any? How
much experience do you have with Python?

I suggest you look at something like unittest’s source code for some
ideas for how to arrange your classes:

There are many sources on the internet showing how to organise a


Thank you very much for your reply. I’m writing python codes from some years but now I’m creating longer codes and projects. I would like, for example, split in many files all the subfunction of a class:

for example:

class Ui(QMainWindow):
def init(self):

 def onButton1Clicked(self):

 def onButton2Clicked(self):

  def runCode1(self,x):

  def runCode2(self,y):


I would like locate all the runCodex in another file.py and import it
The same for all the functions onButtonClicked(self) etc.

Can you help me?
many thanks

have you some suggestions for me?

This isn’t so easy, as an imported file must itself be valid standalone
Python code.

A normal approach would be to define various functions in the other file
and call them from the class method, for example:

    from runners import run_code1, run_code2
    class UI(QMainwindow):
        def runCode1(self, x):
            return run_code1(self, x)

    def run_code1(ui, x):
        ... do something with ui and x ...

This is uncommon because usually a method knows stuff about the class,
and its code is naturally stored inside the class definition.

A more common approach if a class is becoming unweildy may be to
define a “mixin” class, which is a class contianing only methods,
usually quite generic (meaning not reliant on internal aspects of the
main class).


class MadeUpMixin:
    def scale_by(self, x):
        return self.value * x

class UI(QMainWindow, MadeUpMixin):
    def __init__(self, value):
        self.value = value

Here, MadeUpMixin provides a .scale_by(x) method. By subclassing it from
UI the UI class gains all the methods of MadeUpMixin. The mixin class
itself relies only on its superclass having a .value attribute and would
usually know little else; in this way it might be mixed into several
classes as needed.

Then you’d put MadeUpMixin in another file and import it.

Another aspect of Mixin classes is that typically they do not have an
init method, which removed any need for the subclasses to call their
init, because they set up no object state. They just add methods
and assume the object state has already been set up.

Cameron Simpson cs@cskk.id.au

many thanks Cameron