|
PyQt4 中的 Dialog
Dialog 窗口或 dialog 是現代 GUI 應用必不可少的一部分。一個 dialog 定義為兩人或更多人間的會話。在計算機應用中,dialog 就是一個和應用說話的窗口。dialog 可以用于輸入數據,修改數據,更改應用的設置等等。對話框在用戶和計算機的通信間是重要的手段。
QtGui.QInputDialog
QtGui.QInputDialog 提供了一個簡單方便的對話框,用于獲取用戶輸入的一個值。輸入值可以是字符串,數字,或者一個列表中的一項。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we receive data from
a QtGui.QInputDialog dialog.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.btn = QtGui.QPushButton('Dialog', self)
self.btn.move(20, 20)
self.btn.clicked.connect(self.showDialog)
self.le = QtGui.QLineEdit(self)
self.le.move(130, 22)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Input dialog')
self.show()
def showDialog(self):
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
'Enter your name:')
if ok:
self.le.setText(str(text))
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
這個例子中用到了一個按鈕和一個行編輯組件。按鈕會顯示一個輸入對話框用于得到文本。而輸入的文本將在行編輯組件中顯示。
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
'Enter your name:')
這一行顯示了輸入對話框。第一個字符串是對話框的標題,第二個字符串則是對話框中的消息。對話框將返回輸入的文本和一個布爾值。如果點擊了 ok 按鈕,則布爾值為 true ,否則為 false 。
if ok:
self.le.setText(str(text))
從對話框中接收到的文本就被設置到行編輯組件中。
QtGui.QColorDialog
QtGui.QColorDialog 用于選取顏色值。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we select a color value
from the QtGui.QColorDialog and change the background
color of a QtGui.QFrame widget.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
col = QtGui.QColor(0, 0, 0)
self.btn = QtGui.QPushButton('Dialog', self)
self.btn.move(20, 20)
self.btn.clicked.connect(self.showDialog)
self.frm = QtGui.QFrame(self)
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())
self.frm.setGeometry(130, 22, 100, 100)
self.setGeometry(300, 300, 250, 180)
self.setWindowTitle('Color dialog')
self.show()
def showDialog(self):
col = QtGui.QColorDialog.getColor()
if col.isValid():
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
這個例子顯示一個按鈕和一個 QtGui.QFrame 。這個組件的背景被設為黑色。使用 QtGui.QColorDialog 可以改變其背景。
col = QtGui.QColor(0, 0, 0)
這個是 QtGui.QFrame 的初始顏色。
col = QtGui.QColorDialog.getColor()
這一行將彈出 QtGui.QColorDialog 。
if col.isValid():
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())
我們檢查顏色是否合法。如果點擊了取消按鈕,返回的就不是合法值。如果顏色合法,我們就用樣式表更改背景顏色。
QtGui.QFontDialog
QtGui.QFontDialog 用于選取字體。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we select a font name
and change the font of a label.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
vbox = QtGui.QVBoxLayout()
btn = QtGui.QPushButton('Dialog', self)
btn.setSizePolicy(QtGui.QSizePolicy.Fixed,
QtGui.QSizePolicy.Fixed)
btn.move(20, 20)
vbox.addWidget(btn)
btn.clicked.connect(self.showDialog)
self.lbl = QtGui.QLabel('Knowledge only matters', self)
self.lbl.move(130, 20)
vbox.addWidget(self.lbl)
self.setLayout(vbox)
self.setGeometry(300, 300, 250, 180)
self.setWindowTitle('Font dialog')
self.show()
def showDialog(self):
font, ok = QtGui.QFontDialog.getFont()
if ok:
self.lbl.setFont(font)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
在我們的例子中,我們有一個按鈕和一個標簽。通過 QtGui.QFontDialog 我們可以改變標簽的字體。
font, ok = QtGui.QFontDialog.getFont()
我們彈出一個字體對話框。 getFont() 方法將返回字體的名稱和 ok 參數。如果用戶點擊了 OK 那么就是 True ,否則為 False 。
if ok:
self.label.setFont(font)
如果我們點擊了 ok,標簽的字體就可能改變。
QtGui.QFileDialog
QtGui.QFileDialog 是允許用戶選擇文件或目錄的對話框。文件可以用于打開或保存。
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode PyQt4 tutorial
In this example, we select a file with a
QtGui.QFileDialog and display its contents
in a QtGui.QTextEdit.
author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""
import sys
from PyQt4 import QtGui
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.textEdit = QtGui.QTextEdit()
self.setCentralWidget(self.textEdit)
self.statusBar()
openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open new File')
openFile.triggered.connect(self.showDialog)
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('File dialog')
self.show()
def showDialog(self):
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
'/home')
f = open(fname, 'r')
with f:
data = f.read()
self.textEdit.setText(data)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
這個例子中有菜單欄,文本編輯區以及狀態欄。菜單中的選項顯示 QtGui.QFileDialog 用于選擇文件。而文件的內容則載入到文本編輯區。
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
這個例子是基于 QtGui.QMainWindow 組件,因為我們要在中心設置文本編輯區。
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
'/home')
我們彈出 QtGui.QFileDialog 。在 getOpenFileName() 方法中第一個字符串是標題。第二個則是指定對話框的工作目錄。默認情況下,文件過濾為所有文件( * )。
f = open(fname, 'r')
with f:
data = f.read()
self.textEdit.setText(data)
選擇的文件將被讀取,并且其文件內容設置到文本編輯區。
這個部分,我們討論了對話框。
|