#!/usr/bin/python#-*-coding:utf-8-*-"""Zetcode PYQT4 Tutorial In this example, we connect a signalof a qtgui.qslider to a slot of a qtgui.qlcdnumber. Author: Jan Bodnarwebsite:zetcode.com last Edited:october"""ImportSYS fromPyQt4ImportQtgui, QtcoreclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): LCD=Qtgui.qlcdnumber (self) SLD=Qtgui.qslider (QtCore.Qt.Horizontal, self) vbox=qtgui.qvboxlayout () vbox.addwidget (LCD) vbox.addwidget (SLD) self.setlayout (vbox)#Here we connect a valuechanged signal of the slider to the display slot of the LCD number. #The sender is an object, sends a signal. The receiver is the object that receives the signal. The slot is the method, the reacts to the signal.Sld.valueChanged.connect (Lcd.display) self.setgeometry (300, 300, 250, 150) Self.setwindowtitle ('Signal & Slots') 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 reimplement a event handler. Author:jan Bodnarwebsite:zetcode.com last E Dited:october"""ImportSYS fromPyQt4ImportQtgui, QtcoreclassExample (qtgui.qwidget):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): Self.setgeometry (300, 300, 250, 150) Self.setwindowtitle ('Event Handler') self.show ()#in our example, we reimplement the Keypressevent () event handler. defkeypressevent (Self, e):#If We click the Escape button, the application terminates. ifE.key () = =QtCore.Qt.Key_Escape:self.close ()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 determine the event Senderobject.author:Jan Bodnarwebsite:zetcode.com Las T Edited:october"""ImportSYS fromPyQt4ImportQtgui, QtcoreclassExample (Qtgui.qmainwindow):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self): btn1= Qtgui.qpushbutton ("Button 1", self) btn1.move (30, 50) btn2= Qtgui.qpushbutton ("Button 2", self) btn2.move (150, 50) #Both Buttons is connected to the same slot.Btn1.clicked.connect (self.buttonclicked) btn2.clicked.connect (self.buttonclicked) Self.statusbar () self.setgeometry (300, 300, 290, 150) Self.setwindowtitle ('Event Sender') self.show ()defbuttonclicked (self):#We determine the signal source by calling the sender () method. In the statusbar of the application, we show the label of the button being pressed.Sender =Self.sender () Self.statusbar (). ShowMessage (Sender.text ()+'was pressed') 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 show how to emit asignal. Author:jan Bodnarwebsite:zetcode.com Last Edite D:january"""ImportSYS fromPyQt4ImportQtgui, QtcoreclassCommunicate (qtcore.qobject):#a signal is created with the qtcore.pyqtsignal () as A class attribute of the external communicate class.Closeapp =qtcore.pyqtsignal ()classExample (Qtgui.qmainwindow):def __init__(self): Super (Example, self).__init__() Self.initui ()defInitui (self):#The custom Closeapp signal is connected to the close () slot of the Qtgui.qmainwindow.SELF.C =Communicate () Self.c.closeapp.connect (self.close) self.setgeometry (300, 300, 290, 150) Self.setwindowtitle ('Emit Signal') self.show ()#When we click on the window with a mouse pointer, the CLOSEAPP signal is emitted. The application terminates. defmousepressevent (Self, event): Self.c.closeapp.emit ()defMain (): App=qtgui.qapplication (SYS.ARGV) ex=Example () sys.exit (App.exec_ ())if __name__=='__main__': Main ()
Zetcode PYQT4 Tutorial Signals and slots