Update 控件1.md

This commit is contained in:
maicss 2016-10-08 02:30:05 +08:00
parent a0e4b97a3e
commit 5f170672b5

View File

@ -177,45 +177,46 @@ if __name__ == '__main__':
ex = Example()
sys.exit(app.exec_())
```
我们创建了一个切换按钮和一个`QWidget`,并把`QWidget`的背景设置为黑色。点击不同的切换按钮,背景色会在红、绿、蓝之间切换。
我们创建了一个切换按钮和一个`QWidget`,并把`QWidget`的背景设置为黑色。点击不同的切换按钮,背景色会在红、绿、蓝之间切换(而且能看到颜色合成的效果,而不是单纯的颜色覆盖)
```
self.col = QColor(0, 0, 0)
```
This is the initial, black colour value.
设置颜色为黑色。
```
redb = QPushButton('Red', self)
redb.setCheckable(True)
redb.move(10, 10)
```
To create a toggle button, we create a QPushButton and make it checkable by calling the setCheckable() method.
创建一个`QPushButton`,然后调用它的`setCheckable()`的方法就把这个按钮编程了切换按钮。
```
redb.clicked[bool].connect(self.setColor)
```
We connect a clicked signal to our user defined method. We use the clicked signal that operates with a Boolean value.
把点击信号和我们定义好的函数关联起来,这里是把点击事件转换成布尔值。
```
source = self.sender()
```
We get the button which was toggled.
获取被点击的按钮。
```
if source.text() == "Red":
self.col.setRed(val)
```
In case it is a red button, we update the red part of the colour accordingly.
如果是标签为“red”的按钮被点击就把颜色更改为预设好的对应颜色。
```
self.square.setStyleSheet("QFrame { background-color: %s }" %
self.col.name())
```
We use style sheets to change the background colour.
使用样式表就是CSS的SS改变背景色
Toggle button
Figure: Toggle button
程序展示:
![toggle button](./images/6-togglebutton.png)
## QSlider
A QSlider is a widget that has a simple handle. This handle can be pulled back and forth. This way we are choosing a value for a specific task. Sometimes using a slider is more natural than entering a number or using a spin box.
In our example we will show one slider and one label. The label will display an image. The slider will control the label.
`QSlider`是个有一个小滑块的组件这个小滑块能拖着前后滑动这个经常用于修改一些具有范围的数值比文本框或者点击增加减少的文本框spin box方便多了。
本例用一个滑块和一个标签展示。标签为一个图片,滑块控制标签(的值)。
```
#!/usr/bin/python3
# -*- coding: utf-8 -*-
@ -276,27 +277,32 @@ if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
In our example we simulate a volume control. By dragging the handle of a slider, we change an image on the label.
sys.exit(app.exec_())
```
这里是模拟的音量控制器。拖动滑块,能改变标签位置的图片。
```
sld = QSlider(Qt.Horizontal, self)
```
Here we create a horizontal QSlider.
```
self.label = QLabel(self)
self.label.setPixmap(QPixmap('mute.png'))
```
We create a QLabel widget and set an initial mute image to it.
```
sld.valueChanged[int].connect(self.changeValue)
```
We connect the valueChanged signal to the user defined changeValue() method.
```
if value == 0:
self.label.setPixmap(QPixmap('mute.png'))
...
```
Based on the value of the slider, we set an image to the label. In the above code, we set the mute.png image to the label if the slider is equal to zero.
QSlider widget
Figure: QSlider widget
QProgressBar
## QProgressBar
A progress bar is a widget that is used when we process lengthy tasks. It is animated so that the user knows that the task is progressing. The QProgressBar widget provides a horizontal or a vertical progress bar in PyQt5 toolkit. The programmer can set the minimum and maximum value for the progress bar. The default values are 0 and 99.
@ -406,7 +412,7 @@ Inside the doAction() method, we start and stop the timer.
QProgressBar
Figure: QProgressBar
QCalendarWidget
## QCalendarWidget
A QCalendarWidget provides a monthly based calendar widget. It allows a user to select a date in a simple and intuitive way.