I am trying to create a custom widget with a button and a label using the code:
from PySide.QtGui import *
from PySide.QtCore import *
class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__()
self.initUI()
def initUI(self):
self.btn = QPushButton('dia', self)
self.btn.move(20, 20)
self.le = QLineEdit(self)
self.le.move(100, 20)
Following this, I create a class for my main frame and try to display this widget with the code:
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.mw = MyWidget()
self.mw.move(20, 20)
self.resize(250, 300)
self.center()
self.setWindowTitle('Custom Widget Example')
self.show()
When I run this script from the terminal, it shows but its blank. I do not see my widget anywhere.
What do I need to do to get this widget showing?
Thank you.
Why are you using coordinates? Use layouts:
If you want finer control over the disposition of the elements use a grid layout.
If you really want to use “absolute coordinates” then you must always remember to pass the parent widget, otherwise children widgets will not have a place to draw themselves.