0

I have a question that has been around my mind for a while.

Lets say that I have designed my GUI with Qt designer, so I would have the .iu file that I would convert into .py file like this:

pyuic4 mainGUI.ui -o mainGUI.py

So now, If I do this I have all the necessary to access widgets and etc etc

main.py file:

from PyQt4 import QtGui
from mainGUI import Ui_mainGUI

class MyMainWindow(Ui_mainGUI):

    def __init__(self, *args, **kwargs)
        super(MyMainWindow, self).__init__(*args, **kwargs)
        self.setupUi(self)

My question now is that, if I have all my so called worker class in another .py file, how do I integrate all together to make my GUI show results, or show progress etc all these kind of thigs.

A simple-fast example of worker class:

myworker.py file:

import numpy as np
import pandas as pd

class worker():
    def sum2numbers(self, a,b):
        return a+b
    ....
    ....

Should class MyMainWindow inherit?? im a bit confused:

codeKiller
  • 5,493
  • 17
  • 60
  • 115

2 Answers2

1

Just import your worker class module to your main file and use it there. Your MyMainWindow class should also inherit QMainWindow so you can access all the underlying Qt widget methods. Using your example:

from PyQt4 import QtGui
from mainGUI import Ui_mainGUI
from myworker import worker

class MyMainWindow(QMainWindow, Ui_mainGUI):

    def __init__(self, *args, **kwargs)
        super(MyMainWindow, self).__init__(*args, **kwargs)
        self.setupUi(self)
hughg
  • 191
  • 9
  • thanks, I see there are some different options, I just wonder if it is any standard method or it depends on the programmer's preference – codeKiller Jul 30 '15 at 06:02
1

This Link here explains quite well the ways you can connect the Ui file with your main worker file where YourFormName is the name of the file you created from your UI. I personally prefer the single inheritence stated in that link, then you can change your Ui file whenever you want without it affecting your main worker file. Here is what I do.

from Your_ui import Ui_MainWindow
import sys
from PyQt4 import QtGui,QtCore

class Worker(QtGui.QMainWindow):

def __init__(self):
    QtGui.QMainWindow.__init__(self)
    self.ui = Ui_MainWindow() #Creating your Ui object
    self.ui.setupUi(self) #calling your Ui.
    #use a function if you want to modify your ui that designer doesn't provide....
    self.my_worker_stuff()

def my_worker_stuff(self):
    #Now you can access your Ui widgets using the 'self.ui'
    #for eg.
    myText = self.ui.lineEdit.text()

Here is how your __main__ would look

if __name__== '__main__':
    app = QtGui.QApplication(sys.argv)
    worker = Worker()
    worker.show()
    sys.exit(app.exec_())
Community
  • 1
  • 1
Digvijayad
  • 534
  • 6
  • 12
  • thanks, I see there are some options, is it any "better" or any "standard" way for that? or it only depends on what the programmer prefers? – codeKiller Jul 30 '15 at 06:01
  • It depends on your needs. For example if you are connecting your application to a database then you can create a separate file and call both GUI and Database classes inside your worker class. On the most basic level, it is all about preference. I'm not sure what advantage these have over one another on high level. But from my understanding they are pretty much the same. – Digvijayad Jul 30 '15 at 06:32