博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZetCode PyQt4 tutorial signals and slots
阅读量:6242 次
发布时间:2019-06-22

本文共 4882 字,大约阅读时间需要 16 分钟。

#!/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 2011"""import sysfrom PyQt4 import QtGui, QtCoreclass Example(QtGui.QWidget):        def __init__(self):        super(Example, self).__init__()                self.initUI()            def initUI(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 that sends a signal. The receiver is the object that receives the signal. The slot is the method that reacts to the signal.        sld.valueChanged.connect(lcd.display)                self.setGeometry(300, 300, 250, 150)        self.setWindowTitle('Signal & slot')        self.show()        def main():        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 an event handler. author: Jan Bodnarwebsite: zetcode.com last edited: October 2011"""import sysfrom PyQt4 import QtGui, QtCoreclass Example(QtGui.QWidget):        def __init__(self):        super(Example, self).__init__()                self.initUI()            def initUI(self):                      self.setGeometry(300, 300, 250, 150)        self.setWindowTitle('Event handler')        self.show()            # In our example, we reimplement the keyPressEvent() event handler.    def keyPressEvent(self, e):                # If we click the Escape button, the application terminates.        if e.key() == QtCore.Qt.Key_Escape:            self.close()        def main():        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 last edited: October 2011"""import sysfrom PyQt4 import QtGui, QtCoreclass Example(QtGui.QMainWindow):        def __init__(self):        super(Example, self).__init__()                self.initUI()            def initUI(self):              btn1 = QtGui.QPushButton("Button 1", self)        btn1.move(30, 50)        btn2 = QtGui.QPushButton("Button 2", self)        btn2.move(150, 50)              # Both buttons are 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()            def buttonClicked(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')        def main():        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 edited: January 2015"""import sysfrom PyQt4 import QtGui, QtCoreclass Communicate(QtCore.QObject):        # A signal is created with the QtCore.pyqtSignal() as a class attribute of the external Communicate class.    closeApp = QtCore.pyqtSignal()     class Example(QtGui.QMainWindow):        def __init__(self):        super(Example, self).__init__()                self.initUI()                    def initUI(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.    def mousePressEvent(self, event):                self.c.closeApp.emit()                def main():        app = QtGui.QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())if __name__ == '__main__':    main()

 

转载于:https://www.cnblogs.com/zengjfgit/p/4851613.html

你可能感兴趣的文章
聊聊HystrixConcurrencyStrategy
查看>>
PHP多进程系列笔记(一)
查看>>
深析Vue双向数据绑定(MVVM模型)
查看>>
【跃迁之路】【485天】程序员高效学习方法论探索系列(实验阶段242-2018.06.05)...
查看>>
react如果你想为一个组件返回多个元素怎么办?
查看>>
mybatis 为什么每次插入的时候总会创建一个SqlSession?
查看>>
Vue 教程第十六篇—— Vuex 之 action
查看>>
javaScript旋转Base64图片并得到新的base64数据
查看>>
使用opennlp自定义命名实体
查看>>
浅析k8s service的应用
查看>>
Node.js性能分析神器Easy-Monitor
查看>>
css基础—字体那些事
查看>>
性能优化之MySQL调优篇
查看>>
Angular开发实践(七): 跨平台操作DOM及渲染器Renderer2
查看>>
Laravel 教程 - 实战 iBrand 开源电商 API 系统
查看>>
vue-cli的坑,loader重复的锅 Invalid CSS after "...load the styles"
查看>>
手写Spring之IOC基于xml动态创建对象
查看>>
聊聊reactive streams的tranform操作
查看>>
箭头函数与this
查看>>
Angular4学习笔记之DOM属性绑定
查看>>