I have the mainwindow in the ‘DataClass’.How can I create widgets in another class(the HelloClass)
test.py
import sys
import label
from PyQt4 import QtGui, QtCore
class DataClass(QtGui.QMainWindow):
def __init__(self):
super(DataClass, self).__init__()
self.window()
def window(self):
ex=label.HelloClass(self)
ex.print_label()
def main():
app = QtGui.QApplication(sys.argv)
ob=DataClass()
ob.show()
sys.exit(app.exec_())
if __name__=='__main__':
main()
and this is the ‘label.py’ file:
import sys
from PyQt4 import QtGui, QtCore
class HelloClass(QtGui.QMainWindow):
def print_label(self):
self.la=QtGui.QLabel("hello",self)
self.la.move(300,100)
self.la.show()
import sys
from PyQt4 import QtGui, QtCore
class HelloClass(QtGui.QMainWindow):
def print_label(self):
self.la=QtGui.QLabel("hello",self)
self.la.move(300,100)
self.la.show()
You can’t have two
QMainWindowclass, you should just not inherit fromQMainWindowonHelloClass. And if you are setting parent to label then set it yourDataClasswhich is yourQMainWindow.But to be honest, the best way to create GUI using PyQt is to use QtDesigner. Create your .ui file with QtDesigner and then create .py file with command
pyuic4 your.ui -o ui_your.py.— UPDATE —
Your controller class for using gui created by QtDesigner would look like this: