1

I have a main GUI with pushbuttons over it. There is also a sub GUI in a different .py files with a table and pushbuttons over it. I have imported the sub GUI to the main GUI, so now I can show up the sub GUI with a pushbutton press. But, I don't know how to get values from the sub GUI to the main GUI. As my understanding, it's about signal and slots, but the problem is the GUIs are not in the same .py file, so it gives me error every time. Here below is what I'm trying to do:

Sub
....
....
def buttonPressed(self):
   self.value = self.table.selectedItems()[1].text()

   Main.lineEdit.setText(self.value)

Ok here is the updated version of the question with more codes of the project.

#Main.py
...
from Sub import SubUI  
....
class Main(QtGui.QMainWindow, window.Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.pushButtonShow.clicked.connect(self.ShowSub)
    def ShowSub(self):
        self.s = Show(self)
        self.s.show()

.....
...
class Show(QtGui.QMainWindow, SubUI):#class to show SubUI

    def __init__(self, parent = None):
        super(Show, self).__init__(parent)
        self.setupUi(self)





#Sub.py

class SubUI(QtGui.QWidget):

    def __init__(self, main):
        super(SubUI, self).__init__()
        #self.setupUi(self)
        self.Main = main
        self.pushButtonOk.clicked.connect(self.Run)
.....
....
    def Run(self):
        self.value = self.table.selectedItems()[1].text()
        self.Main.lineEdit.setText(self.value)

and here is the error message;

  File "C:...\Sub.py", line 120, in secc
self.Main.lineEdit.setText(self.value)
AttributeError: 'Show' object has no attribute 'Main'

Hope it's more clear now. Thanks!


The solution with a change at Main.py will be something like that;

#Main.py
...
from Sub import SubUI  
....
class Main(QtGui.QMainWindow, window.Ui_MainWindow):
    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)
        self.pushButtonShow.clicked.connect(self.ShowSub)
    def ShowSub(self):
        self.s = SubUI(self)
        self.s.show()
M.Emin
  • 63
  • 7
  • You can show how you create an object of the SubUI class. – eyllanesc Sep 25 '17 at 10:44
  • This instruction does not make sense, why do you inherit 2 widgets? `class Show(QtGui.QMainWindow, SubUI)` – eyllanesc Sep 25 '17 at 10:45
  • This is the way my SubUI shows up with a button click on main.py. If you have a solution, please add yours. Thanks! – M.Emin Sep 25 '17 at 11:04
  • Is there a reason why you need to create the Show class at all? It only seems to be inherting from your SubUI class without any function. Instead inside your Main classes ShowSub function you could do `self.s = SubUI(self)` followed by `self.s.show()` – Marcus Krautwurst Sep 25 '17 at 13:21
  • You are absolutely right! When I have tried your method, I was getting "'SubUI' object has no attribute 'setCentralWidget'", and my SubUI was showing up with a blank window. Now, I have changed my class SubUI(QtGui.QWidget) to -> SubUI(QtGui.QMainWindow) and now it works just perfect! I appreciate your patience and friendly help! Maybe it will be beter to change your class definition in your answer to SubUI(QtGui.QMainWindow) to be more inclusive. – M.Emin Sep 25 '17 at 13:57

1 Answers1

0

You are half way there! All you need to do now is construct your Sub UI class like so

class SubUI(QtGui.QWidget):
    def __init__(self, main):
        super(SubUI, self).__init__()
        self.Main = main

Now when you instance the SubUI from your Main UI you just pass in MainUI as an argument into the __init__ function. If you are calling it from the MainUI that means passing in self. Like so

sub_ui = SubUI(self)

This will then allow you to set the text like you want from within the sub ui

self.Main.lineEdit.setText(self.value)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I have built a class to show the SubUI in my main.py called "Show". So when I press the button in my main GUI, I connect to a function down my Main class and show my SubUI with; self.s = Show(self) ; self.s.show(). I have explained all these because I'm getting an error while I'm doing what you told in your answer; "self.Main.lineEdit.setText(self.value)"..."AttributeError: 'Show' object has no attribute 'Main'. So know I'm a bit confused. Any more help will be appreciated. Thanks! – M.Emin Sep 25 '17 at 07:27