Issue
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()
Solution
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)
Answered By - Marcus Krautwurst
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.