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. HDU5023:A Corrupt Mayor's Performance Art(线段树区域更新+二进制)

    http://acm.hdu.edu.cn/showproblem.php?pid=5023 Problem Description Corrupt governors always find way ...

  2. python 面向对象编程学习总结

    面向对象是个抽象的东西,概念比较多,下面会一一介绍. 一.类和实例 类(Class)和实例(Instance)是面向对象最重要的概念. 类是指抽象出的模板.实例则是根据类创建出来的具体的“对象”,每个 ...

  3. java static成员变量方法和非static成员变量方法的区别

    这里的普通方法和成员变量是指,非静态方法和非静态成员变量首先static是静态的意思,是修饰符,可以被用来修饰变量或者方法. static成员变量有全局变量的作用       非static成员变量则 ...

  4. 19重定向管道与popen模型

    重定向 dup2 int dup(int fd) 重定向文件描述符  int newFd = dup(STDOUT_FILENO) newFd 指向 stdout int dup2(int fd1, ...

  5. Linux系统网络设备启动和禁止“ifconfig eth0 up/down”命令的跟踪

    前面文章讲了Linux系统的ethtool框架的一些东西,是从用户空间可以直观认识到的地方入手.同样,本文从Linux系统绝大部分人都熟悉的“ifconfig eth0 up”命令来跟踪一下此命令在内 ...

  6. Elasticsearch之中文分词器插件es-ik(博主推荐)

    前提 什么是倒排索引? Elasticsearch之分词器的作用 Elasticsearch之分词器的工作流程 Elasticsearch之停用词 Elasticsearch之中文分词器 Elasti ...

  7. Python学习札记(四十三) IO 3

    参考:操作文件和目录 NOTE: 1.Python内置的os模块可以直接调用操作系统提供的接口函数: 2.os.name 打印操作系统的名称:如果是posix,说明系统是Linux.Unix或Mac ...

  8. POJ 3281 Dining(最大流)

    http://poj.org/problem?id=3281 题意: 有n头牛,F种食物和D种饮料,每头牛都有自己喜欢的食物和饮料,每种食物和饮料只能给一头牛,每头牛需要1食物和1饮料.问最多能满足几 ...

  9. UVa 11825 黑客的攻击(状态压缩dp)

    https://vjudge.net/problem/UVA-11825 题意: 假设你是一个黑客,侵入了一个有着n台计算机(编号为0,1,...,n-1)的网络.一共有n种服务,每台计算机都运行着所 ...

  10. 利用HTML中map标签实现整张图片带有可点击区域的图像映射:

    实现效果说明:一整张背景图片,实现图标区域出现链接,可点击跳转到指定页面. <div class="brand"> <img src="images/b ...