对话框
PyQT提供基本的消息框和标准对话框。在PyQt也可以根据需要创建自定义的对话框。
使用PyQt提供的类和方法可以创建和使用消息框、标准对话框等。标准对话框包含基本的打开,关闭,字体选择对话框和颜色选择对话框等。
消息框
使用QtGui.QMessageBox类中的方法可以创建简单的消息框,用于向用户传递信息。QtGui.QMessageBox类中包含一下方法: about():创建关于消息框 aboutQt(): 创建关于Qt消息框 critical(): 创建错误处理对话框 infomation(): 创建信息消息框 question(): 创建询问消息框 warning(): 创建警告消息框
以下是示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #!/usr/bin/env python
#*-* coding=utf-8 *-*
import sys
from PyQt4 import QtGui, QtCore
class MyWindow(QtGui.QWidget):
def __init__(self):
super(MyWindow, self).__init__()
self.setWindowTitle("Lpb_i")
self.resize(300, 200)
gridlayout = QtGui.QGridLayout() # 创建布局组件
self.label = QtGui.QLabel('MessBox example')
gridlayout.addWidget(self.label, 1, 3, 1, 3) #添加标签
self.button1 = QtGui.QPushButton("About") # 生成button1
gridlayout.addWidget(self.button1, 2, 1) # 添加button到布局组件
self.button2 = QtGui.QPushButton("AboutQt") # 生成button2
gridlayout.addWidget(self.button2, 2, 2)
self.button3 = QtGui.QPushButton("Critical")
gridlayout.addWidget(self.button3, 2, 3)
self.button4 = QtGui.QPushButton("Info")
gridlayout.addWidget(self.button4 ,2, 4)
self.button5 = QtGui.QPushButton("Question")
gridlayout.addWidget(self.button5, 2, 5)
self.button6 = QtGui.QPushButton("warning")
gridlayout.addWidget(self.button6, 2, 6)
spacer = QtGui.QSpacerItem(200, 80)
gridlayout.addItem(spacer, 3, 1, 1, 5)
self.setLayout(gridlayout) # 向窗口中添加布局组件
self.connect(self.button1,
QtCore.SIGNAL('clicked()'), self.Onbutton1) # button1事件
self.connect(self.button2,
QtCore.SIGNAL('clicked()'), self.Onbutton2)
self.connect(self.button3,
QtCore.SIGNAL('clicked()'), self.Onbutton3)
self.connect(self.button4,
QtCore.SIGNAL('clicked()'), self.Onbutton4)
self.connect(self.button5,
QtCore.SIGNAL('clicked()'), self.Onbutton5)
self.connect(self.button6,
QtCore.SIGNAL('clicked()'), self.Onbutton6)
def Onbutton1(self): # button1插槽函数
self.button1.setText('clicked')
QtGui.QMessageBox.about(self, 'pyQt', 'About') # 创建About消息框
def Onbutton2(self):
self.button2.setText('clicked')
QtGui.QMessageBox.aboutQt(self, "PyQt")
def Onbutton3(self):
self.button3.setText('clicked')
r = QtGui.QMessageBox.critical(self, "PyQt",
"Critical", QtGui.QMessageBox.Abort,
QtGui.QMessageBox.Retry,
QtGui.QMessageBox.Ignore)
if r == QtGui.QMessageBox.Abort:
self.setWindowTitle("Abort")
elif r == QtGui.QMessageBox.Retry:
self.setWindowTitle("Retry")
else:
self.setWindowTitle("Ignore")
def Onbutton4(self):
self.button4.setText('clicked')
QtGui.QMessageBox.information(self, "Pyqt", "information")
def Onbutton5(self):
self.button5.setText('clicked')
r = QtGui.QMessageBox.question(self, "PyQt",
"Question",
QtGui.QMessageBox.Yes,
QtGui.QMessageBox.No,
QtGui.QMessageBox.Cancel)
def Onbutton6(self):
self.button6.setText('clicked')
r = QtGui.QMessageBox.warning(self, "PyQT",
'warning',
QtGui.QMessageBox.Yes,
QtGui.QMessageBox.No)
app = QtGui.QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
|
运行效果:
标准对话框
PyQt使用QtGui.QFileDialog提供的方法可以创建文件打开、关闭对话框。使用QtGui.QFontDialog可以创建文章选择对话框,使用QtGui.QColorDialog可以创建颜色选择对话框。 其中QtGui.QFileDialog: getExistingDirectory(): 创建选取路径对话框 getOpenFileName(): 创建打开文件对话框 getOpenFileNames(): 创建打开文件对话框,可以同时打开多个文件 getSaveFileNmae(): 创建保存文件对话框
对于QtGui.QFontDialog,静态方法只有getFont,创建字体选择对话框。QtGui.QColorDialog,可以使用getColor创建颜色选择对话框。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #!/usr/bin/env python
#*-* coding=utf-8 *-*
import sys
from PyQt4 import QtGui, QtCore
class MyWindow(QtGui.QWidget):
def __init__(self):
super(MyWindow, self).__init__()
self.setWindowTitle("Lpb_i")
self.resize(300, 200)
gridlayout = QtGui.QGridLayout()
self.label = QtGui.QLabel('StandarDialog example')
gridlayout.addWidget(self.label, 1, 2)
self.button1 = QtGui.QPushButton("File") # 生成button1
gridlayout.addWidget(self.button1, 2, 1)
self.button2 = QtGui.QPushButton("Font") # 生成button2
gridlayout.addWidget(self.button2, 2, 2)
self.button3 = QtGui.QPushButton("Color")
gridlayout.addWidget(self.button3, 2, 3)
spacer = QtGui.QSpacerItem(200, 80)
gridlayout.addItem(spacer, 3,1,1,3)
self.setLayout(gridlayout) # 向窗口添加布局组件
self.connect(self.button1, QtCore.SIGNAL('clicked()'), self.OnButton1)
self.connect(self.button2, QtCore.SIGNAL('clicked()'), self.OnButton2)
self.connect(self.button3, QtCore.SIGNAL('clicked()'), self.OnButton3)
def OnButton1(self):
self.button1.setText('clicked')
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open') # 创建文件打开对话框
if not filename.isEmpty():
self.label.setText(filename)
def OnButton2(self):
self.button2.setText('clicked')
font, ok = QtGui.QFontDialog.getFont() # 创建字体选择对话框
if ok:
self.label.setText(font.key())
def OnButton3(self):
self.button3.setText('clicked')
color = QtGui.QColorDialog.getColor() # 创建颜色选择对话框
if color.isValid():
self.label.setText(color.name())
app = QtGui.QApplication(sys.argv)
win = MyWindow()
win.show()
app.exec_()
|
运行效果:
自定义对话框
通过继承QtGui.QDialog类可以创建自定义的对话框。所创建的对话框和窗口一样,可以向其添加组件。使用connect方法响应组件时间。
自定义对话框例子:
#coding=utf-8
import sys
from PyQt4 import QtGui, QtCore
class MyDialog( QtGui.QDialog ):
def __init__( self ):
super( MyDialog, self ).__init__()
self.gridlayout = QtGui.QGridLayout()
self.label = QtGui.QLabel( "Input:" )
self.gridlayout.addWidget(self.label, 0, 0)
self.textField = QtGui.QLineEdit() # 创建单行文本框
self.gridlayout.addWidget(self.textField, 0,1) # 添加文本框到布局组件
self.okButton = QtGui.QPushButton( "OK" ) # 创建OK按钮
self.gridlayout.addWidget(self.okButton, 1,0) #添加按钮到布局组件
self.cancelButton = QtGui.QPushButton( "Cancel" ) # 创建cancel按钮
self.gridlayout.addWidget(self.cancelButton, 1, 1)
self.setLayout(self.gridlayout)
self.connect( self.okButton, QtCore.SIGNAL( 'clicked()' ), self.OnOk )
self.connect( self.cancelButton, QtCore.SIGNAL( 'clicked()' ), self.OnCancel )
def OnOk( self ):
self.text = self.textField.text() # 获取文本框中的内容
self.done( 1 ) # 结束对话框返回1
def OnCancel( self ):
self.done( 0 )
class Window( QtGui.QWidget ):
def __init__( self ):
super( Window, self ).__init__()
self.setWindowTitle( "Lpb-i" )
self.resize( 300, 200 )
gridlayout = QtGui.QGridLayout()
self.creatDialogButton = QtGui.QPushButton( "Create a new Dialog" ) #生成button
gridlayout.addWidget( self.creatDialogButton, 1, 1)
self.setLayout(gridlayout)
self.connect( self.creatDialogButton, QtCore.SIGNAL( 'clicked()' ), self.OnButton )
def OnButton(self):
dialog = MyDialog()
r = dialog.exec_()
if r:
self.creatDialogButton.setText( dialog.text )
app = QtGui.QApplication( sys.argv )
win = Window()
win.show()
app.exec_()
运行效果:
资源文件
Qt中资源文件是以'.ui'为后缀的文件。Qt提供Qt Designer用于创建资源文件。使用Qt Designer创建的资源文件可以在pyqt中使用。使用资源文件可以简化界面设计,也可以将界面和代码分离,提高程序的可维护性。
示例
#coding=utf-8
import sys
from PyQt4 import QtGui, QtCore, uic
class MyDialog(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
uic.loadUi("res.ui", self) # 载入资源文件
class MyWindow(QtGui.QWidget):
def __init__( self ):
super( MyWindow, self ).__init__()
self.setWindowTitle( "Lpb_i" )
self.resize( 300, 200 )
gridlayout = QtGui.QGridLayout() # 创建布局组件
self.button = QtGui.QPushButton("CreateDialog") # 生成button1
gridlayout.addWidget(self.button, 1, 1)
self.setLayout(gridlayout)
self.connect(self.button,
QtCore.SIGNAL('clicked()'),
self.OnButton)
def OnButton(self):
dialog = MyDialog()
r = dialog.exec_();
if r:
self.button.setText(dialog.lineEdit.text())
app = QtGui.QApplication(sys.argv)
demo = MyWindow()
demo.show()
app.exec_()