[Python][PyQt4]PyQt4 中的 Dialog
PyQt4 中的 Dialog¶
Dialog 窗口或 dialog 是現(xiàn)代 GUI 應(yīng)用必不可少的一部分。一個(gè) dialog 定義為兩人或更多人間的會(huì)話。在計(jì)算機(jī)應(yīng)用中,dialog 就是一個(gè)和應(yīng)用說話的窗口。dialog 可以用于輸入數(shù)據(jù),修改數(shù)據(jù),更改應(yīng)用的設(shè)置等等。對(duì)話框在用戶和計(jì)算機(jī)的通信間是重要的手段。
QtGui.QInputDialog¶
QtGui.QInputDialog 提供了一個(gè)簡(jiǎn)單方便的對(duì)話框,用于獲取用戶輸入的一個(gè)值。輸入值可以是字符串,數(shù)字,或者一個(gè)列表中的一項(xiàng)。
#!/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()
這個(gè)例子中用到了一個(gè)按鈕和一個(gè)行編輯組件。按鈕會(huì)顯示一個(gè)輸入對(duì)話框用于得到文本。而輸入的文本將在行編輯組件中顯示。
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
'Enter your name:')
這一行顯示了輸入對(duì)話框。第一個(gè)字符串是對(duì)話框的標(biāo)題,第二個(gè)字符串則是對(duì)話框中的消息。對(duì)話框?qū)⒎祷剌斎氲奈谋竞鸵粋€(gè)布爾值。如果點(diǎn)擊了 ok 按鈕,則布爾值為 true ,否則為 false 。
if ok:
self.le.setText(str(text))
從對(duì)話框中接收到的文本就被設(shè)置到行編輯組件中。
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()
這個(gè)例子顯示一個(gè)按鈕和一個(gè) QtGui.QFrame 。這個(gè)組件的背景被設(shè)為黑色。使用 QtGui.QColorDialog 可以改變其背景。
col = QtGui.QColor(0, 0, 0)
這個(gè)是 QtGui.QFrame 的初始顏色。
col = QtGui.QColorDialog.getColor()
這一行將彈出 QtGui.QColorDialog 。
if col.isValid():
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())
我們檢查顏色是否合法。如果點(diǎn)擊了取消按鈕,返回的就不是合法值。如果顏色合法,我們就用樣式表更改背景顏色。
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()
在我們的例子中,我們有一個(gè)按鈕和一個(gè)標(biāo)簽。通過 QtGui.QFontDialog 我們可以改變標(biāo)簽的字體。
font, ok = QtGui.QFontDialog.getFont()
我們彈出一個(gè)字體對(duì)話框。 getFont() 方法將返回字體的名稱和 ok 參數(shù)。如果用戶點(diǎn)擊了 OK 那么就是 True ,否則為 False 。
if ok:
self.label.setFont(font)
如果我們點(diǎn)擊了 ok,標(biāo)簽的字體就可能改變。
QtGui.QFileDialog¶
QtGui.QFileDialog 是允許用戶選擇文件或目錄的對(duì)話框。文件可以用于打開或保存。
#!/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()
這個(gè)例子中有菜單欄,文本編輯區(qū)以及狀態(tài)欄。菜單中的選項(xiàng)顯示 QtGui.QFileDialog 用于選擇文件。而文件的內(nèi)容則載入到文本編輯區(qū)。
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
這個(gè)例子是基于 QtGui.QMainWindow 組件,因?yàn)槲覀円谥行脑O(shè)置文本編輯區(qū)。
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
'/home')
我們彈出 QtGui.QFileDialog 。在 getOpenFileName() 方法中第一個(gè)字符串是標(biāo)題。第二個(gè)則是指定對(duì)話框的工作目錄。默認(rèn)情況下,文件過濾為所有文件( * )。
f = open(fname, 'r')
with f:
data = f.read()
self.textEdit.setText(data)
選擇的文件將被讀取,并且其文件內(nèi)容設(shè)置到文本編輯區(qū)。
這個(gè)部分,我們討論了對(duì)話框。
posted on 2012-02-12 10:06 mirguest 閱讀(4887) 評(píng)論(0) 編輯 收藏 引用 所屬分類: Python
只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。 | ||
【推薦】100%開源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
![]() |
||
相關(guān)文章:
|
||
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
|
||
|