1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. ZetCode PyQt4 tutorial
  6.  
  7. In this example, we connect a signal
  8. of a QtGui.QSlider to a slot
  9. of a QtGui.QLCDNumber.
  10.  
  11. author: Jan Bodnar
  12. website: zetcode.com
  13. last edited: October 2011
  14. """
  15.  
  16. import sys
  17. from PyQt4 import QtGui, QtCore
  18.  
  19. class Example(QtGui.QWidget):
  20.  
  21. def __init__(self):
  22. super(Example, self).__init__()
  23.  
  24. self.initUI()
  25.  
  26. def initUI(self):
  27.  
  28. lcd = QtGui.QLCDNumber(self)
  29. sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
  30.  
  31. vbox = QtGui.QVBoxLayout()
  32. vbox.addWidget(lcd)
  33. vbox.addWidget(sld)
  34.  
  35. self.setLayout(vbox)
  36. # Here we connect a valueChanged signal of the slider to the display slot of the lcd number.
  37. # 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.
  38. sld.valueChanged.connect(lcd.display)
  39.  
  40. self.setGeometry(300, 300, 250, 150)
  41. self.setWindowTitle('Signal & slot')
  42. self.show()
  43.  
  44. def main():
  45.  
  46. app = QtGui.QApplication(sys.argv)
  47. ex = Example()
  48. sys.exit(app.exec_())
  49.  
  50. if __name__ == '__main__':
  51. main()
  52.  
  53. --------------------------------------------------------------------------------
  54.  
  55. #!/usr/bin/python
  56. # -*- coding: utf-8 -*-
  57.  
  58. """
  59. ZetCode PyQt4 tutorial
  60.  
  61. In this example, we reimplement an
  62. event handler.
  63.  
  64. author: Jan Bodnar
  65. website: zetcode.com
  66. last edited: October 2011
  67. """
  68.  
  69. import sys
  70. from PyQt4 import QtGui, QtCore
  71.  
  72. class Example(QtGui.QWidget):
  73.  
  74. def __init__(self):
  75. super(Example, self).__init__()
  76.  
  77. self.initUI()
  78.  
  79. def initUI(self):
  80.  
  81. self.setGeometry(300, 300, 250, 150)
  82. self.setWindowTitle('Event handler')
  83. self.show()
  84.  
  85. # In our example, we reimplement the keyPressEvent() event handler.
  86. def keyPressEvent(self, e):
  87.  
  88. # If we click the Escape button, the application terminates.
  89. if e.key() == QtCore.Qt.Key_Escape:
  90. self.close()
  91.  
  92. def main():
  93.  
  94. app = QtGui.QApplication(sys.argv)
  95. ex = Example()
  96. sys.exit(app.exec_())
  97.  
  98. if __name__ == '__main__':
  99. main()
  100.  
  101. --------------------------------------------------------------------------------
  102.  
  103. #!/usr/bin/python
  104. # -*- coding: utf-8 -*-
  105.  
  106. """
  107. ZetCode PyQt4 tutorial
  108.  
  109. In this example, we determine the event sender
  110. object.
  111.  
  112. author: Jan Bodnar
  113. website: zetcode.com
  114. last edited: October 2011
  115. """
  116.  
  117. import sys
  118. from PyQt4 import QtGui, QtCore
  119.  
  120. class Example(QtGui.QMainWindow):
  121.  
  122. def __init__(self):
  123. super(Example, self).__init__()
  124.  
  125. self.initUI()
  126.  
  127. def initUI(self):
  128.  
  129. btn1 = QtGui.QPushButton("Button 1", self)
  130. btn1.move(30, 50)
  131.  
  132. btn2 = QtGui.QPushButton("Button 2", self)
  133. btn2.move(150, 50)
  134.  
  135. # Both buttons are connected to the same slot.
  136. btn1.clicked.connect(self.buttonClicked)
  137. btn2.clicked.connect(self.buttonClicked)
  138.  
  139. self.statusBar()
  140.  
  141. self.setGeometry(300, 300, 290, 150)
  142. self.setWindowTitle('Event sender')
  143. self.show()
  144.  
  145. def buttonClicked(self):
  146.  
  147. # 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.
  148. sender = self.sender()
  149. self.statusBar().showMessage(sender.text() + ' was pressed')
  150.  
  151. def main():
  152.  
  153. app = QtGui.QApplication(sys.argv)
  154. ex = Example()
  155. sys.exit(app.exec_())
  156.  
  157. if __name__ == '__main__':
  158. main()
  159.  
  160. --------------------------------------------------------------------------------
  161.  
  162. #!/usr/bin/python
  163. # -*- coding: utf-8 -*-
  164.  
  165. """
  166. ZetCode PyQt4 tutorial
  167.  
  168. In this example, we show how to emit a
  169. signal.
  170.  
  171. author: Jan Bodnar
  172. website: zetcode.com
  173. last edited: January 2015
  174. """
  175.  
  176. import sys
  177. from PyQt4 import QtGui, QtCore
  178.  
  179. class Communicate(QtCore.QObject):
  180.  
  181. # A signal is created with the QtCore.pyqtSignal() as a class attribute of the external Communicate class.
  182. closeApp = QtCore.pyqtSignal()
  183.  
  184. class Example(QtGui.QMainWindow):
  185.  
  186. def __init__(self):
  187. super(Example, self).__init__()
  188.  
  189. self.initUI()
  190.  
  191. def initUI(self):
  192.  
  193. # The custom closeApp signal is connected to the close() slot of the QtGui.QMainWindow.
  194. self.c = Communicate()
  195. self.c.closeApp.connect(self.close)
  196.  
  197. self.setGeometry(300, 300, 290, 150)
  198. self.setWindowTitle('Emit signal')
  199. self.show()
  200.  
  201. # When we click on the window with a mouse pointer, the closeApp signal is emitted. The application terminates.
  202. def mousePressEvent(self, event):
  203.  
  204. self.c.closeApp.emit()
  205.  
  206. def main():
  207.  
  208. app = QtGui.QApplication(sys.argv)
  209. ex = Example()
  210. sys.exit(app.exec_())
  211.  
  212. if __name__ == '__main__':
  213. main()

ZetCode PyQt4 tutorial signals and slots的更多相关文章

  1. ZetCode PyQt4 tutorial Drag and Drop

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple ...

  2. ZetCode PyQt4 tutorial widgets II

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  3. ZetCode PyQt4 tutorial widgets I

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  4. ZetCode PyQt4 tutorial Dialogs

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  5. ZetCode PyQt4 tutorial layout management

    !/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows ...

  6. ZetCode PyQt4 tutorial work with menus, toolbars, a statusbar, and a main application window

    !/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This program create ...

  7. ZetCode PyQt4 tutorial First programs

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  8. ZetCode PyQt4 tutorial custom widget

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  9. ZetCode PyQt4 tutorial basic painting

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

随机推荐

  1. 在jQuery中解决事件冒泡问题

    <pre name="code" class="html">事件会按照DOM层次结构像水泡一样不断向上直至顶端 解决方法:在事件处理函数中返回fal ...

  2. devise修改密码

    https://ruby-china.org/topics/1314 password/edit不是给你直接改密码用的 这个是忘记密码后,发送重置密码的邮件到你邮箱,同时生成一个token 然后你点那 ...

  3. SQL: 查找空值

    ①用 IS NULL ②NULL 不能用 “=” 运算符 ③NULL 不支持加.减.乘.除.大小比较.相等比较 ④不同的函数对NULL的支持不一样,在遇到NULL时最好测试一下结果会受什么影响,不能仅 ...

  4. DB开发之mysql

    1. MySQL 4.x版本及以上版本提供了全文检索支持,但是表的存储引擎类型必须为MyISAM,以下是建表SQL,注意其中显式设置了存储引擎类型 CREATE TABLE articles ( id ...

  5. POI之Excel导入

    1,maven配置 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-oox ...

  6. Hadoop运维手记

    1.处理hadoop的namenode宕机 处理措施:进入hadoop的bin目录,重启namenode服务 操作命令:cd path/to/hadoop/bin ./hadoop-daemon.sh ...

  7. windows10如何安装cpu版本tensorflow

    1.获取anaconda https://repo.continuum.io/archive/Anaconda3-2018.12-Windows-x86_64.exe (这个版本内置python3.7 ...

  8. Nginx反向代理缓冲区优化

    内容目录 proxy_buffering proxy_buffer_size proxy_buffers proxy_busy_buffers_size proxy_max_temp_file_siz ...

  9. BZOJ 1833 【ZJOI2010】 数字计数

    题目链接:数字计数 没啥好说的,裸裸的数位\(dp\). 先枚举当前是算数字\(x\)出现的次数,设\(f_{i,j}\)表示从高位往低位\(dp\),\(dp\)完了前\(i\)位之后\(x\)出现 ...

  10. 在 R 中使用 Python 字符串函数

    sprintf( )函数很强大,但并非适用于所有应用场景.例如,如果一些部分在模板中多次出现,那么就需要多次写一样的参数.这通常会使得代码冗长而且难以修改:sprintf("%s, %d y ...