This commit is contained in:
maicss 2016-10-08 01:39:59 +08:00
parent c6baa1ce6d
commit b2980a413a
5 changed files with 81 additions and 81 deletions

BIN
images/6-qcheckbox.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
images/6-qprogressbar.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.0 KiB

BIN
images/6-qslider.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

BIN
images/6-togglebutton.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

@ -10,61 +10,61 @@
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
ZetCode PyQt5 tutorial
In this example, we receive data from
a QInputDialog dialog.
a QInputDialog dialog.
author: Jan Bodnar
website: zetcode.com
website: zetcode.com
last edited: January 2015
"""
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
QInputDialog, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
def initUI(self):
self.btn = QPushButton('Dialog', self)
self.btn.move(20, 20)
self.btn.clicked.connect(self.showDialog)
self.le = QLineEdit(self)
self.le.move(130, 22)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Input dialog')
self.show()
def showDialog(self):
text, ok = QInputDialog.getText(self, 'Input Dialog',
text, ok = QInputDialog.getText(self, 'Input Dialog',
'Enter your name:')
if ok:
self.le.setText(str(text))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
这个示例有一个按钮和一个输入框,点击按钮显示对话框,输入的文本会显示在输入框里。
```
text, ok = QInputDialog.getText(self, 'Input Dialog',
text, ok = QInputDialog.getText(self, 'Input Dialog',
'Enter your name:')
```
这是显示一个输入框的代码。第一个参数是输入框的标题第二个参数是输入框的占位符。对话框返回输入内容和一个布尔值如果点击的是OK按钮布尔值就返回True。
@ -86,34 +86,34 @@ QColorDialog提供颜色的选择。
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
ZetCode PyQt5 tutorial
In this example, we select a color value
from the QColorDialog and change the background
color of a QFrame widget.
color of a QFrame widget.
author: Jan Bodnar
website: zetcode.com
website: zetcode.com
last edited: January 2015
"""
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame,
from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame,
QColorDialog, QApplication)
from PyQt5.QtGui import QColor
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
col = QColor(0, 0, 0)
self.initUI()
def initUI(self):
col = QColor(0, 0, 0)
self.btn = QPushButton('Dialog', self)
self.btn.move(20, 20)
@ -121,33 +121,33 @@ class Example(QWidget):
self.btn.clicked.connect(self.showDialog)
self.frm = QFrame(self)
self.frm.setStyleSheet("QWidget { background-color: %s }"
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())
self.frm.setGeometry(130, 22, 100, 100)
self.frm.setGeometry(130, 22, 100, 100)
self.setGeometry(300, 300, 250, 180)
self.setWindowTitle('Color dialog')
self.show()
def showDialog(self):
col = QColorDialog.getColor()
if col.isValid():
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
例子里有一个按钮和一个QFrame默认的背景颜色为黑色我们可以使用`QColorDialog`改变背景颜色。
```
col = QColor(0, 0, 0)
col = QColor(0, 0, 0)
```
初始化`QtGui.QFrame`的背景颜色。
```
@ -157,7 +157,7 @@ col = QColorDialog.getColor()
```
if col.isValid():
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())
% col.name())
```
我们可以预览颜色,如果点击取消按钮,没有颜色值返回,如果颜色是我们想要的,就从取色框里选择这个颜色。
@ -173,63 +173,63 @@ todo截图
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
ZetCode PyQt5 tutorial
In this example, we select a font name
and change the font of a label.
and change the font of a label.
author: Jan Bodnar
website: zetcode.com
website: zetcode.com
last edited: January 2015
"""
import sys
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
QSizePolicy, QLabel, QFontDialog, QApplication)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
def initUI(self):
vbox = QVBoxLayout()
btn = QPushButton('Dialog', self)
btn.setSizePolicy(QSizePolicy.Fixed,
QSizePolicy.Fixed)
btn.move(20, 20)
vbox.addWidget(btn)
btn.clicked.connect(self.showDialog)
self.lbl = QLabel('Knowledge only matters', self)
self.lbl.move(130, 20)
vbox.addWidget(self.lbl)
self.setLayout(vbox)
self.setLayout(vbox)
self.setGeometry(300, 300, 250, 180)
self.setWindowTitle('Font dialog')
self.show()
def showDialog(self):
font, ok = QFontDialog.getFont()
if ok:
self.lbl.setFont(font)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
@ -258,32 +258,32 @@ if ok:
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
ZetCode PyQt5 tutorial
In this example, we select a file with a
QFileDialog and display its contents
in a QTextEdit.
author: Jan Bodnar
website: zetcode.com
website: zetcode.com
last edited: January 2015
"""
import sys
from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
QAction, QFileDialog, QApplication)
from PyQt5.QtGui import QIcon
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
def initUI(self):
self.textEdit = QTextEdit()
self.setCentralWidget(self.textEdit)
@ -296,13 +296,13 @@ class Example(QMainWindow):
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile)
fileMenu.addAction(openFile)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('File dialog')
self.show()
def showDialog(self):
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
@ -312,28 +312,28 @@ class Example(QMainWindow):
with f:
data = f.read()
self.textEdit.setText(data)
self.textEdit.setText(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
The example shows a menubar, centrally set text edit widget, and a statusbar. The menu item shows the QtGui.QFileDialog which is used to select a file. The contents of the file are loaded into the text edit widget.
本例中有一个菜单栏,一个置中的文本编辑框,一个状态栏。点击菜单栏选项会弹出一个`QtGui.QFileDialog`对话框,在这个对话框里,你能选择文件,然后文件的内容就会显示在文本编辑框里。
```
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
```
The example is based on the QMainWindow widget because we centrally set a text edit widget.
这里设置了一个文本编辑框,文本编辑框是基于`QMainWindow`组件的。
```
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
```
We pop up the QFileDialog. The first string in the getOpenFileName() method is the caption. The second string specifies the dialog working directory. By default, the file filter is set to All files (*).
弹出`QFileDialog`窗口。`getOpenFileName()`方法的第一个参数是说明文字,第二个参数是默认打开的文件夹路径。默认情况下显示所有类型的文件。
```
if fname[0]:
f = open(fname[0], 'r')
@ -342,7 +342,7 @@ if fname[0]:
data = f.read()
self.textEdit.setText(data)
```
The selected file name is read and the contents of the file are set to the text edit widget.
读取选中的文件并显示在文本编辑框内但是打开HTML文件时是渲染后的结果
程序展示: