Advertisement
furas

Pokazywanie i chowanie własnych widgetów w PyQt

Nov 10th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. from PyQt4 import QtCore, QtGui
  5.  
  6.  
  7. class SimpleCalc(QtGui.QWidget):
  8.  
  9.     def __init__(self, parent=None):
  10.         QtGui.QWidget.__init__(self, parent)
  11.  
  12.         self.widget1 = QtGui.QLabel('Widget1')
  13.  
  14.         layout = QtGui.QVBoxLayout()
  15.         layout.addWidget(self.widget1)
  16.         self.setLayout(layout)
  17.        
  18.  
  19. class ExtendedCalc(QtGui.QWidget):
  20.  
  21.     def __init__(self, parent=None):
  22.         QtGui.QWidget.__init__(self, parent)
  23.  
  24.         self.widget1 = QtGui.QLabel('Widget1')
  25.         self.widget2 = QtGui.QLabel('Widget2')
  26.         self.widget3 = QtGui.QLabel('Widget3')
  27.  
  28.         layout = QtGui.QVBoxLayout()
  29.         layout.addWidget(self.widget1)
  30.         layout.addWidget(self.widget2)
  31.         layout.addWidget(self.widget3)
  32.         self.setLayout(layout)
  33.        
  34.  
  35. class MainWidget(QtGui.QWidget):
  36.  
  37.     def __init__(self, parent=None):
  38.         QtGui.QWidget.__init__(self, parent)
  39.        
  40.         self.button = QtGui.QPushButton('Change')
  41.         self.button.clicked.connect(self.change_widgets)
  42.  
  43.         self.widget1 = SimpleCalc()
  44.         self.widget2 = ExtendedCalc()
  45.        
  46.         self.widget2.hide()
  47.  
  48.         layout = QtGui.QVBoxLayout()
  49.         layout.addWidget(self.button)
  50.         layout.addWidget(self.widget1)
  51.         layout.addWidget(self.widget2)
  52.         self.setLayout(layout)
  53.  
  54.         self.resize(640, 180)
  55.         self.show()
  56.  
  57.     def change_widgets(self):
  58.         if self.widget1.isHidden():
  59.             self.widget1.show()
  60.             self.widget2.hide()
  61.             self.resize(640, 180)
  62.         else:
  63.             self.widget1.hide()
  64.             self.widget2.show()
  65.             self.resize(640, 280)
  66.  
  67.  
  68. if __name__ == "__main__":
  69.     app = QtGui.QApplication(sys.argv)
  70.     widget = MainWidget()
  71.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement