ZetCode PyQt4 tutorial widgets I
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- """
- ZetCode PyQt4 tutorial
- In this example, a QtGui.QCheckBox widget
- is used to toggle the title of a window.
- author: Jan Bodnar
- website: zetcode.com
- last edited: September 2011
- """
- import sys
- from PyQt4 import QtGui, QtCore
- class Example(QtGui.QWidget):
- def __init__(self):
- super(Example, self).__init__()
- self.initUI()
- def initUI(self):
- # This is a QtGui.QCheckBox constructor.
- cb = QtGui.QCheckBox('Show title', self)
- cb.move(20, 20)
- # 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.
- cb.toggle()
- # We connect the user defined changeTitle() method to the stateChanged signal. The changeTitle() method will toggle the window title.
- cb.stateChanged.connect(self.changeTitle)
- self.setGeometry(300, 300, 250, 150)
- self.setWindowTitle('QtGui.QCheckBox')
- self.show()
- # 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.
- def changeTitle(self, state):
- if state == QtCore.Qt.Checked:
- self.setWindowTitle('QtGui.QCheckBox')
- else:
- self.setWindowTitle('')
- 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 create three toggle buttons.
- They will control the background color of a
- QtGui.QFrame.
- author: Jan Bodnar
- website: zetcode.com
- last edited: September 2011
- """
- import sys
- from PyQt4 import QtGui
- class Example(QtGui.QWidget):
- def __init__(self):
- super(Example, self).__init__()
- self.initUI()
- def initUI(self):
- # This is the initial, black colour value.
- self.col = QtGui.QColor(0, 0, 0)
- # To create a toggle button, we create a QtGui.QPushButton and make it checkable by calling the setCheckable() method.
- redb = QtGui.QPushButton('Red', self)
- redb.setCheckable(True)
- redb.move(10, 10)
- # We connect a clicked signal to our user defined method. We use the clicked signal that operates with a Boolean value.
- redb.clicked[bool].connect(self.setColor)
- greenb = QtGui.QPushButton('Green', self)
- greenb.setCheckable(True)
- greenb.move(10, 60)
- greenb.clicked[bool].connect(self.setColor)
- blueb = QtGui.QPushButton('Blue', self)
- blueb.setCheckable(True)
- blueb.move(10, 110)
- blueb.clicked[bool].connect(self.setColor)
- self.square = QtGui.QFrame(self)
- self.square.setGeometry(150, 20, 100, 100)
- self.square.setStyleSheet("QWidget { background-color: %s }" %
- self.col.name())
- self.setGeometry(300, 300, 280, 170)
- self.setWindowTitle('Toggle button')
- self.show()
- def setColor(self, pressed):
- # We get the button which was toggled.
- source = self.sender()
- if pressed:
- val = 255
- else: val = 0
- # In case it is a red button, we update the red part of the colour accordingly.
- if source.text() == "Red":
- self.col.setRed(val)
- elif source.text() == "Green":
- self.col.setGreen(val)
- else:
- self.col.setBlue(val)
- # We use style sheets to change the background colour.
- self.square.setStyleSheet("QFrame { background-color: %s }" %
- self.col.name())
- 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
- This example shows a QtGui.QSlider widget.
- author: Jan Bodnar
- website: zetcode.com
- last edited: September 2011
- """
- import sys
- from PyQt4 import QtGui, QtCore
- class Example(QtGui.QWidget):
- def __init__(self):
- super(Example, self).__init__()
- self.initUI()
- def initUI(self):
- # Here we create a horizontal QtGui.QSlider.
- sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
- sld.setFocusPolicy(QtCore.Qt.NoFocus)
- sld.setGeometry(30, 40, 100, 30)
- # We connect the valueChanged signal to the user defined changeValue() method.
- sld.valueChanged[int].connect(self.changeValue)
- self.label = QtGui.QLabel(self)
- # We create a QtGui.QLabel widget and set an initial mute image to it.
- self.label.setPixmap(QtGui.QPixmap('mute.png'))
- self.label.setGeometry(160, 40, 80, 30)
- self.setGeometry(300, 300, 280, 170)
- self.setWindowTitle('QtGui.QSlider')
- self.show()
- def changeValue(self, value):
- # 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
- if value == 0:
- self.label.setPixmap(QtGui.QPixmap('mute.png'))
- elif value > 0 and value <= 30:
- self.label.setPixmap(QtGui.QPixmap('min.png'))
- elif value > 30 and value < 80:
- self.label.setPixmap(QtGui.QPixmap('med.png'))
- else:
- self.label.setPixmap(QtGui.QPixmap('max.png'))
- 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
- This example shows a QtGui.QProgressBar widget.
- author: Jan Bodnar
- website: zetcode.com
- last edited: September 2011
- """
- import sys
- from PyQt4 import QtGui, QtCore
- class Example(QtGui.QWidget):
- def __init__(self):
- super(Example, self).__init__()
- self.initUI()
- def initUI(self):
- # This is a QtGui.QProgressBar constructor.
- self.pbar = QtGui.QProgressBar(self)
- self.pbar.setGeometry(30, 40, 200, 25)
- self.btn = QtGui.QPushButton('Start', self)
- self.btn.move(40, 80)
- self.btn.clicked.connect(self.doAction)
- # To activate the progress bar, we use a timer object.
- self.timer = QtCore.QBasicTimer()
- self.step = 0
- self.setGeometry(300, 300, 280, 170)
- self.setWindowTitle('QtGui.QProgressBar')
- self.show()
- # Each QtCore.QObject and its descendants have a timerEvent() event handler. In order to react to timer events, we reimplement the event handler.
- def timerEvent(self, e):
- if self.step >= 100:
- self.timer.stop()
- self.btn.setText('Finished')
- return
- self.step = self.step + 1
- self.pbar.setValue(self.step)
- # Inside the doAction() method, we start and stop the timer.
- def doAction(self):
- if self.timer.isActive():
- self.timer.stop()
- self.btn.setText('Start')
- else:
- # 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.
- self.timer.start(100, self)
- self.btn.setText('Stop')
- 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
- This example shows a QtGui.QCalendarWidget widget.
- author: Jan Bodnar
- website: zetcode.com
- last edited: September 2011
- """
- import sys
- from PyQt4 import QtGui, QtCore
- class Example(QtGui.QWidget):
- def __init__(self):
- super(Example, self).__init__()
- self.initUI()
- def initUI(self):
- # We construct a calendar widget.
- cal = QtGui.QCalendarWidget(self)
- cal.setGridVisible(True)
- cal.move(20, 20)
- # 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.
- cal.clicked[QtCore.QDate].connect(self.showDate)
- self.lbl = QtGui.QLabel(self)
- date = cal.selectedDate()
- self.lbl.setText(date.toString())
- self.lbl.move(130, 260)
- self.setGeometry(300, 300, 350, 300)
- self.setWindowTitle('Calendar')
- self.show()
- # 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.
- def showDate(self, date):
- self.lbl.setText(date.toString())
- def main():
- app = QtGui.QApplication(sys.argv)
- ex = Example()
- sys.exit(app.exec_())
- if __name__ == '__main__':
- main()
ZetCode PyQt4 tutorial widgets I的更多相关文章
- ZetCode PyQt4 tutorial widgets II
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial layout management
!/usr/bin/python -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This example shows ...
- ZetCode PyQt4 tutorial First programs
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial Drag and Drop
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial This is a simple ...
- ZetCode PyQt4 tutorial Dialogs
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial signals and slots
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- 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 ...
- ZetCode PyQt4 tutorial custom widget
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial basic painting
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
随机推荐
- HDU5023:A Corrupt Mayor's Performance Art(线段树区域更新+二进制)
http://acm.hdu.edu.cn/showproblem.php?pid=5023 Problem Description Corrupt governors always find way ...
- python 面向对象编程学习总结
面向对象是个抽象的东西,概念比较多,下面会一一介绍. 一.类和实例 类(Class)和实例(Instance)是面向对象最重要的概念. 类是指抽象出的模板.实例则是根据类创建出来的具体的“对象”,每个 ...
- java static成员变量方法和非static成员变量方法的区别
这里的普通方法和成员变量是指,非静态方法和非静态成员变量首先static是静态的意思,是修饰符,可以被用来修饰变量或者方法. static成员变量有全局变量的作用 非static成员变量则 ...
- 19重定向管道与popen模型
重定向 dup2 int dup(int fd) 重定向文件描述符 int newFd = dup(STDOUT_FILENO) newFd 指向 stdout int dup2(int fd1, ...
- Linux系统网络设备启动和禁止“ifconfig eth0 up/down”命令的跟踪
前面文章讲了Linux系统的ethtool框架的一些东西,是从用户空间可以直观认识到的地方入手.同样,本文从Linux系统绝大部分人都熟悉的“ifconfig eth0 up”命令来跟踪一下此命令在内 ...
- Elasticsearch之中文分词器插件es-ik(博主推荐)
前提 什么是倒排索引? Elasticsearch之分词器的作用 Elasticsearch之分词器的工作流程 Elasticsearch之停用词 Elasticsearch之中文分词器 Elasti ...
- Python学习札记(四十三) IO 3
参考:操作文件和目录 NOTE: 1.Python内置的os模块可以直接调用操作系统提供的接口函数: 2.os.name 打印操作系统的名称:如果是posix,说明系统是Linux.Unix或Mac ...
- POJ 3281 Dining(最大流)
http://poj.org/problem?id=3281 题意: 有n头牛,F种食物和D种饮料,每头牛都有自己喜欢的食物和饮料,每种食物和饮料只能给一头牛,每头牛需要1食物和1饮料.问最多能满足几 ...
- UVa 11825 黑客的攻击(状态压缩dp)
https://vjudge.net/problem/UVA-11825 题意: 假设你是一个黑客,侵入了一个有着n台计算机(编号为0,1,...,n-1)的网络.一共有n种服务,每台计算机都运行着所 ...
- 利用HTML中map标签实现整张图片带有可点击区域的图像映射:
实现效果说明:一整张背景图片,实现图标区域出现链接,可点击跳转到指定页面. <div class="brand"> <img src="images/b ...