PyQt4入門學習筆記(五)


PyQt4裏的對話框

對話框是大多數GUI應用中不可分割的一部分。一個對話框是二者或多者的會話。在GUI內,對話框是應用向人說話的方式。一個對話框能夠用來輸入數據,修改數據,改變應用設置等等。python


QtGui.QInputDialog

QtGui.QInputDialog給用戶提供了一個簡單方便的對話框來獲取值。輸入的值能夠使字符串,一個數字,或者是一個列表中的元素。web

#!/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()

例子有一個按鈕和一行編輯控件。點擊按鈕顯示處獲得文本值的對話框。輸入的值將會在編輯控件裏顯示出來。app

text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 
    'Enter your name:')

這行代碼顯示了輸入對話框。第一個字符串是對話框的標題(title),第二個字符串是對話框裏的信息。對話框返回輸入的文本值和一個boolean值。若是咱們點擊OK按鈕,布爾值將會爲真。字體

if ok:
    self.le.setText(str(text))

這行代碼使應用將對話框收到的文本值放到編輯控件上。
image1ui


QtGui.QColorDialog

QtGui.QColorDialog提供了一個選擇顏色的對話框this

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example, we select a colour value
from the QtGui.QColorDialog and change the background
colour 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,咱們能夠改變背景顏色。code

col = QtGui.QColor(0, 0, 0)

這是QtGui.QFrame的初始背景顏色。blog

col = QtGui.QColorDialog.getColor()

這一行將會彈出選擇顏色的界面。
image3ip

if col.isValid():
    self.frm.setStyleSheet("QWidget { background-color: %s }"
        % col.name())

咱們檢查顏色是否合法,若是咱們點擊取消鍵,不會返回合法的顏色。若是顏色是合法的,咱們改變背景顏色。
image2utf-8


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()

image4

在咱們的例子裏,咱們有一個按鈕和一個標籤,咱們能夠改變標籤的字體。

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)

上面這段代碼讀文件,而且將文件內容載入到文本編輯控件中。
image5

相關文章
相關標籤/搜索