Python -- Gui编程 -- Qt库的使用 -- 菜单与对话框

1.菜单

 import sys
 from PyQt4 import QtCore, QtGui

 class MyWindow(QtGui.QMainWindow):
     def __init__(self):
         QtGui.QMainWindow.__init__(self)
         self.setWindowTitle('QtMenu')
         self.resize(485, 300)

         menubar = self.menuBar()
         file = menubar.addMenu('&File')
         file.addAction('Open')
         file.addAction('Save')
         file.addSeparator()
         file.addAction('Close')

         edit = menubar.addMenu('&Edit')
         edit.addAction('Copy')
         edit.addAction('Paste')
         edit.addAction('Cut')
         edit.addAction('Select All')

         help = menubar.addMenu('&Help')
         help.addAction('About')

 app = QtGui.QApplication(sys.argv)
 mywindow = MyWindow()
 mywindow.show()
 app.exec_()

qtMenu.py

Python -- Gui编程 -- Qt库的使用 -- 菜单与对话框

2.菜单事件

 import sys
 from PyQt4 import QtGui, QtCore

 class MyWindow(QtGui.QMainWindow):
     def __init__(self):
         QtGui.QMainWindow.__init__(self)
         self.setWindowTitle('QtMenuAction')
         self.resize(485, 300)
         self.lbl = QtGui.QLabel('Menu Action')
         self.lbl.setAlignment(QtCore.Qt.AlignCenter)
         self.setCentralWidget(self.lbl)

         menubar = self.menuBar()
         self.file = menubar.addMenu('&File')
         self.open = self.file.addAction('Open')
         self.save = self.file.addAction('Save')
         self.file.addSeparator()
         self.close = self.file.addAction('Close')

         self.connect(self.open, QtCore.SIGNAL('triggered()'), self.onOpen)
         self.connect(self.save, QtCore.SIGNAL('triggered()'), self.onSave)
         self.connect(self.close, QtCore.SIGNAL('triggered()'), self.onClose)

     def onOpen(self):
         self.lbl.setText('Menu Action: Open')
     def onSave(self):
         self.lbl.setText('Menu Action: Save')
     def onClose(self):
         self.lbl.setText('Menu Action: Close')
     def contextMenuEvent(self, event):
         self.file.exec_(event.globalPos())

 app = QtGui.QApplication(sys.argv)
 mywindow = MyWindow()
 mywindow.show()
 app.exec_()

qtMenuAction.py

Python -- Gui编程 -- Qt库的使用 -- 菜单与对话框

3.消息框

 import sys
 from PyQt4 import QtGui, QtCore

 class MyWindow(QtGui.QWidget):
     def __init__(self):
         QtGui.QWidget.__init__(self)
         self.setWindowTitle('QtMessageBox')
         self.resize(485, 300)

         self.btn1 = QtGui.QPushButton('About')
         self.btn2 = QtGui.QPushButton('About Qt')
         self.btn3 = QtGui.QPushButton('Critical')
         self.btn4 = QtGui.QPushButton('Information')
         self.btn5 = QtGui.QPushButton('Question')
         self.btn6 = QtGui.QPushButton('Warning')

         grid = QtGui.QGridLayout()
         grid.addWidget(self.btn1, 0, 0)
         grid.addWidget(self.btn2, 0, 1)
         grid.addWidget(self.btn3, 0, 2)
         grid.addWidget(self.btn4, 1, 0)
         grid.addWidget(self.btn5, 1, 1)
         grid.addWidget(self.btn6, 1, 2)
         self.setLayout(grid)

         self.connect(self.btn1, QtCore.SIGNAL('clicked()'), self.onBtn1)
         self.connect(self.btn2, QtCore.SIGNAL('clicked()'), self.onBtn2)
         self.connect(self.btn3, QtCore.SIGNAL('clicked()'), self.onBtn3)
         self.connect(self.btn4, QtCore.SIGNAL('clicked()'), self.onBtn4)
         self.connect(self.btn5, QtCore.SIGNAL('clicked()'), self.onBtn5)
         self.connect(self.btn6, QtCore.SIGNAL('clicked()'), self.onBtn6)

     def onBtn1(self):
         QtGui.QMessageBox.about(self, 'Qt', 'about')
     def onBtn2(self):
         QtGui.QMessageBox.aboutQt(self, 'Qt')
     def onBtn3(self):
         QtGui.QMessageBox.critical(self, 'Qt', 'critical', QtGui.QMessageBox.Abort, QtGui.QMessageBox.Retry, QtGui.QMessageBox.Ignore)
     def onBtn4(self):
         QtGui.QMessageBox.information(self, 'Qt', 'information')
     def onBtn5(self):
         QtGui.QMessageBox.question(self, 'Qt', 'question', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No, QtGui.QMessageBox.Cancel)
     def onBtn6(self):
         QtGui.QMessageBox.warning(self, 'Qt', 'warinng', QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)

 app = QtGui.QApplication(sys.argv)
 mywindow = MyWindow()
 mywindow.show()
 app.exec_()

pyMessageBox.py

Python -- Gui编程 -- Qt库的使用 -- 菜单与对话框

4.标准对话框

 import sys
 from PyQt4 import QtGui, QtCore

 class MyWindow(QtGui.QWidget):
     def __init__(self):
         QtGui.QWidget.__init__(self)
         self.setWindowTitle('QtStandardDialog')
         self.resize(485, 300)

         self.lbl = QtGui.QLabel()
         self.btn1 = QtGui.QPushButton('File')
         self.btn2 = QtGui.QPushButton('Font')
         self.btn3 = QtGui.QPushButton('Color')

         grid = QtGui.QGridLayout()
         grid.addWidget(self.lbl, 0, 0, 1, 3)
         grid.addWidget(self.btn1, 1, 0)
         grid.addWidget(self.btn2, 1, 1)
         grid.addWidget(self.btn3, 1, 2)
         self.setLayout(grid)

         self.connect(self.btn1, QtCore.SIGNAL('clicked()'), self.onBtn1)
         self.connect(self.btn2, QtCore.SIGNAL('clicked()'), self.onBtn2)
         self.connect(self.btn3, QtCore.SIGNAL('clicked()'), self.onBtn3)

     def onBtn1(self):
         filename = QtGui.QFileDialog.getOpenFileName(self, 'open')
         if filename:
             self.lbl.setText(filename)
     def onBtn2(self):
         font, ok = QtGui.QFontDialog.getFont()
         if ok:
             self.lbl.setText(font.key())
     def onBtn3(self):
         color = QtGui.QColorDialog.getColor()
         if color.isValid():
             self.lbl.setText(color.name())

 app = QtGui.QApplication(sys.argv)
 mywindow = MyWindow()
 mywindow.show()
 app.exec_()

qtStandardDialog.py

Python -- Gui编程 -- Qt库的使用 -- 菜单与对话框

5.自定义对话框

 import sys
 from PyQt4 import QtCore, QtGui

 class MyDialog(QtGui.QDialog):
     def __init__(self):
         QtGui.QDialog.__init__(self)

         self.lbl = QtGui.QLabel('Input:')
         self.txt = QtGui.QLineEdit()
         self.btn1 = QtGui.QPushButton('OK')
         self.btn2 = QtGui.QPushButton('Cancel')

         grid = QtGui.QGridLayout()
         grid.addWidget(self.lbl, 0, 0)
         grid.addWidget(self.txt, 0, 1)
         grid.addWidget(self.btn1, 1, 0)
         grid.addWidget(self.btn2, 1, 1)
         self.setLayout(grid)

         self.connect(self.btn1, QtCore.SIGNAL('clicked()'), self.onBtn1)
         self.connect(self.btn2, QtCore.SIGNAL('clicked()'), self.onBtn2)

     def onBtn1(self):
         self.text = self.txt.text()
         self.done(1)
     def onBtn2(self):
         self.done(0)

 class MyWindow(QtGui.QMainWindow):
     def __init__(self):
         QtGui.QMainWindow.__init__(self)
         self.setWindowTitle('QtDialog')
         self.resize(485, 300)

         self.btn = QtGui.QPushButton('Click Here')
         self.setCentralWidget(self.btn)

         self.connect(self.btn, QtCore.SIGNAL('clicked()'), self.onBtn)

     def onBtn(self):
         mydialog = MyDialog()
         r = mydialog.exec_()
         if r:
             self.btn.setText(mydialog.text)

 app = QtGui.QApplication(sys.argv)
 mywindow = MyWindow()
 mywindow.show()
 app.exec_()

pyDialog.py

Python -- Gui编程 -- Qt库的使用 -- 菜单与对话框

上一篇:Java转义emoji等特殊符号


下一篇:【编程题目】在一个字符串中找到第一个只出现一次的字符。如输入 abaccdeff,则输出 b。