1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. ZetCode PyQt4 tutorial
  6.  
  7. In this example, a QtGui.QCheckBox widget
  8. is used to toggle the title of a window.
  9.  
  10. author: Jan Bodnar
  11. website: zetcode.com
  12. last edited: September 2011
  13. """
  14.  
  15. import sys
  16. from PyQt4 import QtGui, QtCore
  17.  
  18. class Example(QtGui.QWidget):
  19.  
  20. def __init__(self):
  21. super(Example, self).__init__()
  22.  
  23. self.initUI()
  24.  
  25. def initUI(self):
  26.  
  27. # This is a QtGui.QCheckBox constructor.
  28. cb = QtGui.QCheckBox('Show title', self)
  29. cb.move(20, 20)
  30. # We have set the window title, so we must also check the checkbox. By default, the window title is not set and the checkbox is unchecked.
  31. cb.toggle()
  32. # We connect the user defined changeTitle() method to the stateChanged signal. The changeTitle() method will toggle the window title.
  33. cb.stateChanged.connect(self.changeTitle)
  34.  
  35. self.setGeometry(300, 300, 250, 150)
  36. self.setWindowTitle('QtGui.QCheckBox')
  37. self.show()
  38.  
  39. # The state of the widget is given to the changeTitle() method in the state variable. If the widget is checked, we set a title of the window. Otherwise, we set an empty string to the titlebar.
  40. def changeTitle(self, state):
  41.  
  42. if state == QtCore.Qt.Checked:
  43. self.setWindowTitle('QtGui.QCheckBox')
  44. else:
  45. self.setWindowTitle('')
  46.  
  47. def main():
  48.  
  49. app = QtGui.QApplication(sys.argv)
  50. ex = Example()
  51. sys.exit(app.exec_())
  52.  
  53. if __name__ == '__main__':
  54. main()
  55.  
  56. --------------------------------------------------------------------------------
  57.  
  58. #!/usr/bin/python
  59. # -*- coding: utf-8 -*-
  60.  
  61. """
  62. ZetCode PyQt4 tutorial
  63.  
  64. In this example, we create three toggle buttons.
  65. They will control the background color of a
  66. QtGui.QFrame.
  67.  
  68. author: Jan Bodnar
  69. website: zetcode.com
  70. last edited: September 2011
  71. """
  72.  
  73. import sys
  74. from PyQt4 import QtGui
  75.  
  76. class Example(QtGui.QWidget):
  77.  
  78. def __init__(self):
  79. super(Example, self).__init__()
  80.  
  81. self.initUI()
  82.  
  83. def initUI(self):
  84.  
  85. # This is the initial, black colour value.
  86. self.col = QtGui.QColor(0, 0, 0)
  87.  
  88. # To create a toggle button, we create a QtGui.QPushButton and make it checkable by calling the setCheckable() method.
  89. redb = QtGui.QPushButton('Red', self)
  90. redb.setCheckable(True)
  91. redb.move(10, 10)
  92.  
  93. # We connect a clicked signal to our user defined method. We use the clicked signal that operates with a Boolean value.
  94. redb.clicked[bool].connect(self.setColor)
  95.  
  96. greenb = QtGui.QPushButton('Green', self)
  97. greenb.setCheckable(True)
  98. greenb.move(10, 60)
  99.  
  100. greenb.clicked[bool].connect(self.setColor)
  101.  
  102. blueb = QtGui.QPushButton('Blue', self)
  103. blueb.setCheckable(True)
  104. blueb.move(10, 110)
  105.  
  106. blueb.clicked[bool].connect(self.setColor)
  107.  
  108. self.square = QtGui.QFrame(self)
  109. self.square.setGeometry(150, 20, 100, 100)
  110. self.square.setStyleSheet("QWidget { background-color: %s }" %
  111. self.col.name())
  112.  
  113. self.setGeometry(300, 300, 280, 170)
  114. self.setWindowTitle('Toggle button')
  115. self.show()
  116.  
  117. def setColor(self, pressed):
  118.  
  119. # We get the button which was toggled.
  120. source = self.sender()
  121.  
  122. if pressed:
  123. val = 255
  124. else: val = 0
  125.  
  126. # In case it is a red button, we update the red part of the colour accordingly.
  127. if source.text() == "Red":
  128. self.col.setRed(val)
  129. elif source.text() == "Green":
  130. self.col.setGreen(val)
  131. else:
  132. self.col.setBlue(val)
  133.  
  134. # We use style sheets to change the background colour.
  135. self.square.setStyleSheet("QFrame { background-color: %s }" %
  136. self.col.name())
  137.  
  138. def main():
  139.  
  140. app = QtGui.QApplication(sys.argv)
  141. ex = Example()
  142. sys.exit(app.exec_())
  143.  
  144. if __name__ == '__main__':
  145. main()
  146.  
  147. --------------------------------------------------------------------------------
  148.  
  149. #!/usr/bin/python
  150. # -*- coding: utf-8 -*-
  151.  
  152. """
  153. ZetCode PyQt4 tutorial
  154.  
  155. This example shows a QtGui.QSlider widget.
  156.  
  157. author: Jan Bodnar
  158. website: zetcode.com
  159. last edited: September 2011
  160. """
  161.  
  162. import sys
  163. from PyQt4 import QtGui, QtCore
  164.  
  165. class Example(QtGui.QWidget):
  166.  
  167. def __init__(self):
  168. super(Example, self).__init__()
  169.  
  170. self.initUI()
  171.  
  172. def initUI(self):
  173.  
  174. # Here we create a horizontal QtGui.QSlider.
  175. sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
  176. sld.setFocusPolicy(QtCore.Qt.NoFocus)
  177. sld.setGeometry(30, 40, 100, 30)
  178. # We connect the valueChanged signal to the user defined changeValue() method.
  179. sld.valueChanged[int].connect(self.changeValue)
  180.  
  181. self.label = QtGui.QLabel(self)
  182. # We create a QtGui.QLabel widget and set an initial mute image to it.
  183. self.label.setPixmap(QtGui.QPixmap('mute.png'))
  184. self.label.setGeometry(160, 40, 80, 30)
  185.  
  186. self.setGeometry(300, 300, 280, 170)
  187. self.setWindowTitle('QtGui.QSlider')
  188. self.show()
  189.  
  190. def changeValue(self, value):
  191.  
  192. # Based on the value of the slider, we set an image to the label. In the above code, we set a mute.png image to the label if the slider is equal to zero
  193. if value == 0:
  194. self.label.setPixmap(QtGui.QPixmap('mute.png'))
  195. elif value > 0 and value <= 30:
  196. self.label.setPixmap(QtGui.QPixmap('min.png'))
  197. elif value > 30 and value < 80:
  198. self.label.setPixmap(QtGui.QPixmap('med.png'))
  199. else:
  200. self.label.setPixmap(QtGui.QPixmap('max.png'))
  201.  
  202. def main():
  203.  
  204. app = QtGui.QApplication(sys.argv)
  205. ex = Example()
  206. sys.exit(app.exec_())
  207.  
  208. if __name__ == '__main__':
  209. main()
  210.  
  211. --------------------------------------------------------------------------------
  212.  
  213. #!/usr/bin/python
  214. # -*- coding: utf-8 -*-
  215.  
  216. """
  217. ZetCode PyQt4 tutorial
  218.  
  219. This example shows a QtGui.QProgressBar widget.
  220.  
  221. author: Jan Bodnar
  222. website: zetcode.com
  223. last edited: September 2011
  224. """
  225.  
  226. import sys
  227. from PyQt4 import QtGui, QtCore
  228.  
  229. class Example(QtGui.QWidget):
  230.  
  231. def __init__(self):
  232. super(Example, self).__init__()
  233.  
  234. self.initUI()
  235.  
  236. def initUI(self):
  237.  
  238. # This is a QtGui.QProgressBar constructor.
  239. self.pbar = QtGui.QProgressBar(self)
  240. self.pbar.setGeometry(30, 40, 200, 25)
  241.  
  242. self.btn = QtGui.QPushButton('Start', self)
  243. self.btn.move(40, 80)
  244. self.btn.clicked.connect(self.doAction)
  245.  
  246. # To activate the progress bar, we use a timer object.
  247. self.timer = QtCore.QBasicTimer()
  248. self.step = 0
  249.  
  250. self.setGeometry(300, 300, 280, 170)
  251. self.setWindowTitle('QtGui.QProgressBar')
  252. self.show()
  253.  
  254. # Each QtCore.QObject and its descendants have a timerEvent() event handler. In order to react to timer events, we reimplement the event handler.
  255. def timerEvent(self, e):
  256.  
  257. if self.step >= 100:
  258.  
  259. self.timer.stop()
  260. self.btn.setText('Finished')
  261. return
  262.  
  263. self.step = self.step + 1
  264. self.pbar.setValue(self.step)
  265.  
  266. # Inside the doAction() method, we start and stop the timer.
  267. def doAction(self):
  268.  
  269. if self.timer.isActive():
  270. self.timer.stop()
  271. self.btn.setText('Start')
  272.  
  273. else:
  274. # To launch a timer event, we call its start() method. This method has two parameters: the timeout and the object which will receive the events.
  275. self.timer.start(100, self)
  276. self.btn.setText('Stop')
  277.  
  278. def main():
  279.  
  280. app = QtGui.QApplication(sys.argv)
  281. ex = Example()
  282. sys.exit(app.exec_())
  283.  
  284. if __name__ == '__main__':
  285. main()
  286.  
  287. --------------------------------------------------------------------------------
  288.  
  289. #!/usr/bin/python
  290. # -*- coding: utf-8 -*-
  291.  
  292. """
  293. ZetCode PyQt4 tutorial
  294.  
  295. This example shows a QtGui.QCalendarWidget widget.
  296.  
  297. author: Jan Bodnar
  298. website: zetcode.com
  299. last edited: September 2011
  300. """
  301.  
  302. import sys
  303. from PyQt4 import QtGui, QtCore
  304.  
  305. class Example(QtGui.QWidget):
  306.  
  307. def __init__(self):
  308. super(Example, self).__init__()
  309.  
  310. self.initUI()
  311.  
  312. def initUI(self):
  313.  
  314. # We construct a calendar widget.
  315. cal = QtGui.QCalendarWidget(self)
  316. cal.setGridVisible(True)
  317. cal.move(20, 20)
  318. # If we select a date from the widget, a clicked[QtCore.QDate] signal is emitted. We connect this signal to the user defined showDate() method.
  319. cal.clicked[QtCore.QDate].connect(self.showDate)
  320.  
  321. self.lbl = QtGui.QLabel(self)
  322. date = cal.selectedDate()
  323. self.lbl.setText(date.toString())
  324. self.lbl.move(130, 260)
  325.  
  326. self.setGeometry(300, 300, 350, 300)
  327. self.setWindowTitle('Calendar')
  328. self.show()
  329.  
  330. # We retrieve the selected date by calling the selectedDate() method. Then we transform the date object into string and set it to the label widget.
  331. def showDate(self, date):
  332.  
  333. self.lbl.setText(date.toString())
  334.  
  335. def main():
  336.  
  337. app = QtGui.QApplication(sys.argv)
  338. ex = Example()
  339. sys.exit(app.exec_())
  340.  
  341. if __name__ == '__main__':
  342. main()

ZetCode PyQt4 tutorial widgets I的更多相关文章

  1. ZetCode PyQt4 tutorial widgets II

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

  2. ZetCode PyQt4 tutorial layout management

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

  3. ZetCode PyQt4 tutorial First programs

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

  4. ZetCode PyQt4 tutorial Drag and Drop

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

  5. ZetCode PyQt4 tutorial Dialogs

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

  6. ZetCode PyQt4 tutorial signals and slots

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

  7. 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 ...

  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. 028-B+树(一)

    B+ 树 这部分主要学习:什么是B+树? 了解了 B 树后再来了解下它的变形版:B+ 树,它比 B 树的查询性能更高. 一棵 B+ 树需要满足以下条件: 节点的子树数和关键字数相同(B 树是关键字数比 ...

  2. 通过Python来操作kylin

    起因: 老大要求的数据,无法通过kylin里面的SQL查询到,只能通过调用接口来实现需求 第一步,安装依赖的包(py2/py3都支持,我这边用的是py2) pip install kylinpy pi ...

  3. iOS error: -34018

    一般报这个错误是由于操作keychain 报的错. 遇到该情况的情况: 1.是否打开权限 2.苹果自身的bug,传送门:https://stackoverflow.com/questions/2974 ...

  4. STM32之独立版USB(Host)驱动+MSC+Fatfs移植

    源:STM32之独立版USB(Host)驱动+MSC+Fatfs移植 STM32之USB驱动库详解(架构+文件+函数+使用说明+示例程序)

  5. Python面试题之Python反射详解

    0x00 前言 反射,可以理解为利用字符串的形式去对象中操作成员属性和方法 反射的这点特性让我联想到了exec函数,也是把利用字符串的形式去让Python解释器去执行命令 Python Version ...

  6. SQL学习笔记之MySQL查询练习2

    (网络搜集) 0x00 数据准备 CREATE TABLE students (sno ) NOT NULL, sname ) NOT NULL, ssex ) NOT NULL, sbirthday ...

  7. asp.net web api的源码

    从安装的NuGet packages逆向找回去 <package id="Microsoft.AspNet.WebApi.Core" version="5.2.7& ...

  8. luogu p3371 单源最短路径(dijkstral

    本来我写的对的 我就多手写了个 ios::sync_with_stdio(false); 我程序里面用了cin 还有scanf 本来想偷偷懒 我就说 我查了半天错 根本找不到的啊... 后来交了几次 ...

  9. adb 安装软件

    一.连接 adb connect 192.168.1.10 输出 connected to 二.查看设备 adb devices 输出 List of devices attached device ...

  10. RabbitMQ入门_02_HelloWorld

    A. AMQP基础 RabbitMQ 并不是基于 Java 开发人员熟悉的 JMS 规范设计开发的,而是基于一个比 JMS 更新更合理的 AMQP (Advanced Message Queuing ...