【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事

时间:2021-10-16 04:46:25

如何使用PyQT

  • 创建窗口
# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('PyQt')
        self.resize(300, 200)
app = QtGui.QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 组件
* 标签

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.setWindowTitle('PyQt') self.resize(300, 200) label = QtGui.QLabel('PyQt\nLabel') label.setAlignment(QtCore.Qt.AlignCenter) self.setCentralWidget(label) app = QtGui.QApplication(sys.argv) mywindow = MyWindow() mywindow.show() app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 布局组件和空白项

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.setWindowTitle('PyQt') self.resize(300, 200) gridlayout = QtGui.QGridLayout() hboxlayout1 = QtGui.QHBoxLayout() hboxlayout2 = QtGui.QHBoxLayout() vboxlayout1 = QtGui.QVBoxLayout() vboxlayout2 = QtGui.QVBoxLayout() label1 = QtGui.QLabel('Label1', self) label1.setAlignment(QtCore.Qt.AlignCenter) label2 = QtGui.QLabel('Label2') label3 = QtGui.QLabel('Label3') label4 = QtGui.QLabel('Label4') label5 = QtGui.QLabel('Label5') hboxlayout1.addWidget(label1) vboxlayout1.addWidget(label2) vboxlayout1.addWidget(label3) vboxlayout2.addWidget(label4) vboxlayout2.addWidget(label5) hboxlayout2.addLayout(vboxlayout1) hboxlayout2.addLayout(vboxlayout2) gridlayout.addLayout(hboxlayout1, 0, 0) gridlayout.addLayout(hboxlayout2, 1, 0) gridlayout.setRowMinimumHeight(1, 180) self.setLayout(gridlayout) app = QtGui.QApplication(sys.argv) mywindow = MyWindow() mywindow.show() app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.setWindowTitle('PyQt') self.resize(300, 200) gridlayout = QtGui.QGridLayout() spacer1 = QtGui.QSpacerItem(300, 40) spacer2 = QtGui.QSpacerItem(300, 40) label = QtGui.QLabel('Label', self) label.setAlignment(QtCore.Qt.AlignCenter) gridlayout.addItem(spacer1, 0, 0) gridlayout.addWidget(label, 1, 0) gridlayout.addItem(spacer2, 2, 0) self.setLayout(gridlayout) app = QtGui.QApplication(sys.argv) mywindow = MyWindow() mywindow.show() app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 按钮

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle('PyQt')
        self.resize(300, 200)
        gridlayout = QtGui.QGridLayout()
        button1 = QtGui.QPushButton("Button1")
        gridlayout.addWidget(button1, 1, 1, 1, 3)
        button2 = QtGui.QPushButton('Button2')
        button2.setFlat(True)
        gridlayout.addWidget(button2, 2, 2)
        self.setLayout(gridlayout)
app = QtGui.QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 信号和插槽

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle('PyQt')
        self.resize(300, 200)
        gridlayout = QtGui.QGridLayout()
        self.button1 = QtGui.QPushButton("Button1")
        gridlayout.addWidget(self.button1, 1, 1, 1, 3)
        self.button2 = QtGui.QPushButton('Button2')
        # self.button2.setFlat(True)
        gridlayout.addWidget(self.button2, 2, 2)
        self.setLayout(gridlayout)
        self.connect(self.button1,
            QtCore.SIGNAL('clicked()'),
            self.OnButton1)
        self.connect(self.button2,
            QtCore.SIGNAL('clicked()'),
            self.OnButton2)
    def OnButton1(self):
        self.button1.setText('clicked')
    def OnButton2(self):
        self.button2.setText('clicked')
app = QtGui.QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 文本框
* 单行文本框

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.setWindowTitle('PyQt') self.resize(300, 200) gridlayout = QtGui.QGridLayout() label1 = QtGui.QLabel('Normal Lineedit') label1.setAlignment(QtCore.Qt.AlignCenter) gridlayout.addWidget(label1, 0, 0) edit1 = QtGui.QLineEdit() gridlayout.addWidget(edit1, 1, 0) label2 = QtGui.QLabel('Password') label2.setAlignment(QtCore.Qt.AlignCenter) gridlayout.addWidget(label2, 2, 0) edit2 = QtGui.QLineEdit() edit2.setEchoMode(QtGui.QLineEdit.Password) gridlayout.addWidget(edit2, 3, 0) self.setLayout(gridlayout) app = QtGui.QApplication(sys.argv) mywindow = MyWindow() mywindow.show() app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 多行文本框

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.setWindowTitle('PyQt') self.resize(300, 200) gridlayout = QtGui.QGridLayout() label = QtGui.QLabel('TextEdit') label.setAlignment(QtCore.Qt.AlignCenter) gridlayout.addWidget(label, 0, 0) edit = QtGui.QTextEdit() edit.setText('Python\nPyQt') gridlayout.addWidget(edit, 1, 0) self.setLayout(gridlayout) app = QtGui.QApplication(sys.argv) mywindow = MyWindow() mywindow.show() app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 单选框和复选框

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.setWindowTitle('PyQt') self.resize(300, 200) gridlayout = QtGui.QGridLayout() self.label1 = QtGui.QLabel('Label1') self.label2 = QtGui.QLabel('Label2') gridlayout.addWidget(self.label1, 1, 2) gridlayout.addWidget(self.label2, 2, 2) self.radio1 = QtGui.QRadioButton('Radio1') self.radio2 = QtGui.QRadioButton('Radio2') self.radio3 = QtGui.QRadioButton('Radio3') self.radio1.setChecked(True) gridlayout.addWidget(self.radio1, 1, 1) gridlayout.addWidget(self.radio2, 2, 1) gridlayout.addWidget(self.radio3, 3, 1) self.check = QtGui.QCheckBox('check') self.check.setChecked(True) gridlayout.addWidget(self.check, 3, 2) self.button = QtGui.QPushButton('Test') gridlayout.addWidget(self.button, 4, 1, 1,2) self.setLayout(gridlayout) self.connect(self.button, QtCore.SIGNAL('clicked()'), self.OnButton) def OnButton(self): if self.radio1.isChecked(): self.radio1.setText('Radio1') elif self.radio2.isChecked(): self.radio1.setText('Radio2') else: self.label1.setText('Radio3') if self.check.isChecked(): self.label2.setText('checked') else: self.label2.SetText('uncheck') app = QtGui.QApplication(sys.argv) mywindow = MyWindow() mywindow.show() app.exec_() 

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 菜单
* 创建菜单

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.setWindowTitle('PyQt') self.resize(300, 200) 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('SelectAll') help = menubar.addMenu('&Help') help.addAction('About') app = QtGui.QApplication(sys.argv) mywindow = MyWindow() mywindow.show() app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 菜单事件

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('PyQt')
        self.resize(300, 200)
        self.label = QtGui.QLabel('Menu Action')
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.setCentralWidget(self.label)
        menubar = self.menuBar()
        self.file = menubar.addMenu('&File')
        open = self.file.addAction('Open')
        self.connect(open, QtCore.SIGNAL('triggered()'),self.OnOpen)
        save = self.file.addAction('Save')
        self.connect(save, QtCore.SIGNAL('triggered()'), self.OnSave)
        self.file.addSeparator()
        close = self.file.addAction('Close')
        self.connect(close, QtCore.SIGNAL('triggered()'),self.OnClose)
    def OnOpen(self):
        self.label.setText('Menu Action: Open')
    def OnSave(self):
        self.label.setText('Menu Action: Save')
    def OnClose(self):
        self.close()
    def contextMenuEvent(self, event):
        self.file.exec_(event.globalPos())     
app = QtGui.QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_() 

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事

  • 对话框
    • 消息框和标准对话框
# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle('PyQt')
        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')
        gridlayout.addWidget(self.button1, 2, 1)
        self.button2 = QtGui.QPushButton('AboutQt')
        gridlayout.addWidget(self.button2, 2, 2)
        self.button3 = QtGui.QPushButton('Critical')   
        gridlayout.addWidget(self.button3, 2, 3)
        self.button4 = QtGui.QPushButton('About')
        gridlayout.addWidget(self.button4, 2, 4)
        self.button5 = QtGui.QPushButton('AboutQt')
        gridlayout.addWidget(self.button5, 2, 5)
        self.button6 = QtGui.QPushButton('Critical')   
        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)
        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):
        self.button1.setText('clicked')
        QtGui.QMessageBox.about(self, 'PyQt', '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.label.setText('About')
        elif r == QtGui.QMessageBox.Retry:
                    self.label.setText('Retry')
        else:
                    self.label.setText('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)
mywindow = MyWindow()
mywindow.show()
app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle('PyQt')
        self.resize(300, 200)
        gridlayout = QtGui.QGridLayout()
        self.label = QtGui.QLabel('StandardDialog example')
        gridlayout.addWidget(self.label, 1, 2)
        self.button1 = QtGui.QPushButton('File')
        gridlayout.addWidget(self.button1, 2, 1)
        self.button2 = QtGui.QPushButton('Font')
        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)
mywindow = MyWindow()
mywindow.show()
app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
* 自定义对话框

# -*- coding:utf-8 -*-
#
import sys
from PyQt4 import QtCore, QtGui
class MyDialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.gridlayout = QtGui.QGridLayout()
        self.label = QtGui.QLabel('Input')
        self.gridlayout.addWidget(self.label, 0, 0)
        self.edit = QtGui.QLineEdit()
        self.gridlayout.addWidget(self.edit, 0, 1)
        self.ok = QtGui.QPushButton('Ok')
        self.gridlayout.addWidget(self.ok, 1, 0)
        self.cancel = QtGui.QPushButton('Cancel')  
        self.gridlayout.addWidget(self.cancel, 1, 1)
        self.setLayout(self.gridlayout)
        self.connect(self.ok, 
                    QtCore.SIGNAL('clicked()'),
                    self.OnOk)
        self.connect(self.cancel, 
                    QtCore.SIGNAL('clicked()'),
                    self.OnCancel)
    def OnOk(self):
        self.text = self.edit.text()
        self.done(1)
    def OnCancel(self):
        self.done(0)                                      
class MyWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setWindowTitle('PyQt')
        self.resize(300, 200)
        gridlayout = QtGui.QGridLayout()
        self.button = QtGui.QPushButton('CreateDialog')
        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.text)
app = QtGui.QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
app.exec_()

【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事
【脚本语言系列】关于PythonGUI界面PyQT,你需要知道的事

什么是PyQT

PyQt是对Qt的封装。Qt是面向对象的图形用户界面库,可以在多个操作系统上使用。
与其他的开源GUI库相比,Qt库显得过于庞大。
另外,Qt虽然是开源GUI库,但是其许可证限制复杂。