!/usr/bin/python-*-Coding:utf-8-*-"""Zetcode PYQT4 Tutorial This example shows three labels on a windowusing absolute positioning. Author:jan Bodnarwebsit E:zetcode.com last Edited:october"""ImportSYS fromPyQt4ImportQtguiclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self):#The label widget is positioned at x=15 and y=10.LBL1 = Qtgui.qlabel ('Zetcode', self) lbl1.move (15, 10) Lbl2= Qtgui.qlabel ('Tutorials', self) lbl2.move (35, 40) Lbl3= Qtgui.qlabel ('For Programmers', self) lbl3.move (55, 70) Self.setgeometry (300, 300, 250, 150) Self.setwindowtitle ('Absolute') self.show ()defMain (): App=qtgui.qapplication (SYS.ARGV) ex=Example () sys.exit (App.exec_ ())if __name__=='__main__': Main ()--------------------------------------------------------------------------------#!/usr/bin/python#-*-coding:utf-8-*-"""Zetcode PYQT4 Tutorial In this example, we position both pushbuttons in the Bottom-right corner of the window. Author: Jan Bodnarwebsite:zetcode.com last Edited:october"""ImportSYS fromPyQt4ImportQtguiclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self):#Here we create both push buttons.OKButton = Qtgui.qpushbutton ("OK") CancelButton= Qtgui.qpushbutton ("Cancel") #We Create a horizontal box layout and add a stretch factor and both buttons. The stretch adds a stretchable space before the both buttons. This would push them to the right of the window.Hbox =qtgui.qhboxlayout ()#Adds a stretchable space (a qspaceritem) with zero minimum size and stretch factor stretch to the end of this box lay Out. #Add a spring to fill the spaceHbox.addstretch (1) Hbox.addwidget (okbutton) hbox.addwidget (CancelButton)#to create the necessary layout, we put a horizontal layout into a vertical one. The stretch factor in the vertical box would push the horizontal box with the buttons to the bottom of the window.VBox =qtgui.qvboxlayout ()#Add a spring to fill the spaceVbox.addstretch (1) vbox.addlayout (Hbox)#Finally, we set the main layout of the window.self.setlayout (VBox) self.setgeometry (300, 300, 300, 150) Self.setwindowtitle ('Buttons') self.show ()defMain (): App=qtgui.qapplication (SYS.ARGV) ex=Example () sys.exit (App.exec_ ())if __name__=='__main__': Main ()--------------------------------------------------------------------------------#!/usr/bin/python#-*-coding:utf-8-*-ImportSYS fromPyQt4ImportQtgui"""Zetcode PYQT4 Tutorial In this example, we create a skeletonof a calculator using a QtGui.QGridLayout.author:Jan Bod Narwebsite:zetcode.com last Edited:july"""classExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self):#The instance of a qtgui.qgridlayout is created and set to being the layout for the application window.Grid =qtgui.qgridlayout () self.setlayout (GRID)#These is the labels used later for buttons.names = ['Cls','Bck',"','Close', '7','8','9','/', '4','5','6','*', '1','2','3','-', '0','.','=','+'] #We Create a list of positions in the grid.positions = [(I,J) forIinchRange (5) forJinchRange (4)] #Buttons is created and added to the layout with the AddWidget () method. forPosition, nameinchZip (positions, names):ifName = ="': ContinueButton=Qtgui.qpushbutton (name) grid.addwidget (button,*position) Self.move (300, 150) Self.setwindowtitle ('Calculator') self.show ()defMain (): App=qtgui.qapplication (SYS.ARGV) ex=Example () sys.exit (App.exec_ ())if __name__=='__main__': Main ()--------------------------------------------------------------------------------#!/usr/bin/python#-*-coding:utf-8-*-"""Zetcode PYQT4 Tutorial In this example, we create a bitmore complicated window layout usingthe qtgui.qgridlayout manag Er. Author:jan Bodnarwebsite:zetcode.com last Edited:october"""ImportSYS fromPyQt4ImportQtguiclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): title= Qtgui.qlabel ('Title') Author= Qtgui.qlabel ('Author') Review= Qtgui.qlabel ('Review') Titleedit=qtgui.qlineedit () Authoredit=qtgui.qlineedit () Reviewedit=Qtgui.qtextedit ()#We Create a grid layout and set spacing between widgets.Grid =qtgui.qgridlayout () grid.setspacing (10) Grid.addwidget (title,1, 0) grid.addwidget (titleedit,1, 1) Grid.addwidget (author,2, 0) grid.addwidget (authoredit,2, 1) Grid.addwidget (review,3, 0)#If We add a widget to a grid, we can provide row span and column span of the widget. We make the Reviewedit widget span 5 rows.Grid.addwidget (Reviewedit, 3, 1, 5, 1) self.setlayout (GRID) Self.setgeometry (300, 300, 350, 300) Self.setwindowtitle ('Review') self.show ()defMain (): App=qtgui.qapplication (SYS.ARGV) ex=Example () sys.exit (App.exec_ ())if __name__=='__main__': Main ()
Zetcode PYQT4 Tutorial Layout Management