Python——GUI编程(python programming)

Python——GUI编程(python programming)

import sys
from math import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Form(QDialog):
    def __init__(self,parent=None):
        super().__init__(parent)
        
        layout = QVBoxLayout()
        
        self.setLayout(layout)
        
    def updateUi(self):
        pass

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

QLabel

import sys
from math import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Form(QDialog):
    def __init__(self,parent=None):
        super().__init__(parent)

        #标签
        self.aLabel = QLabel(self)
        self.aLabel.setText("hhhhhhh")

        #数字框
        self.mySpinBox = QDoubleSpinBox(self)
        self.mySpinBox.setValue(100)
        self.mySpinBox.setRange(1,1000)

        #滑动条
        self.s = QSlider()

        #可编辑文本
        self.lineedit = QLineEdit("请输入我爱帅帅:")

        #不可编辑文本框
        self.browser = QTextBrowser()
        self.browser.append("你改不了吧,嘿嘿")

        #选单
        self.listWidget = QListWidget(self)
        self.listWidget.addItems(["巧克力","抹茶","冰淇淋"])

        #下拉选单
        self.comboBox = QComboBox(self)
        itemdata = ['学习python','学习c++','学习java']
        self.comboBox.addItems(itemdata)

        #按钮
        self.okButton = QPushButton(self)
        self.okButton.setText("我佛了...")
        
        layout = QVBoxLayout()
        layout.addWidget(self.aLabel)
        layout.addWidget(self.mySpinBox)
        layout.addWidget(self.s)
        layout.addWidget(self.lineedit)
        layout.addWidget(self.browser)
        layout.addWidget(self.listWidget)
        layout.addWidget(self.comboBox)
        layout.addWidget(self.okButton)
        
        self.setLayout(layout)
        
    def updateUi(self):
        pass

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

 set_pen

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class PenPropertiesDlg(QDialog):
    def __init__(self, parent=None):
        super(PenPropertiesDlg, self).__init__(parent)
        
        widthLabel = QLabel("&Width:")
        self.widthSpinBox = QSpinBox()
        widthLabel.setBuddy(self.widthSpinBox)
        
        self.widthSpinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
        self.widthSpinBox.setRange(0, 24)
        self.beveledCheckBox = QCheckBox("&Beveled edges")
        
        styleLabel = QLabel("&Style:")
        self.styleComboBox = QComboBox()
        styleLabel.setBuddy(self.styleComboBox)
        self.styleComboBox.addItems(["Solid", "Dashed", "Dotted", "DashDotted", "DashDotDotted"])

        okButton = QPushButton("&OK")
        cancelButton = QPushButton("Cancel")

        buttonLayout = QHBoxLayout()
        buttonLayout.addStretch()
        buttonLayout.addWidget(okButton)
        buttonLayout.addWidget(cancelButton)
        layout = QGridLayout()
        layout.addWidget(widthLabel, 0, 0)
        layout.addWidget(self.widthSpinBox, 0, 1)
        layout.addWidget(self.beveledCheckBox, 0, 2)
        layout.addWidget(styleLabel, 1, 0)
        layout.addWidget(self.styleComboBox, 1, 1, 1, 2)
        layout.addLayout(buttonLayout, 2, 0, 1, 3)
        self.setLayout(layout)
        self.setWindowTitle("Pen Properties")

        okButton.clicked.connect(self.accept)
        cancelButton.clicked.connect(self.reject)


class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)
        self.width = 1
        self.beveled = False
        self.style = "Solid"

        penButton = QPushButton("Set Pen")
        self.label = QLabel("The Pen has not been set")
        self.label.setTextFormat(Qt.RichText)
        layout = QVBoxLayout()
        layout.addWidget(penButton)
        layout.addWidget(self.label)
        self.setLayout(layout)
        self.setWindowTitle("Pen")
        penButton.clicked.connect(self.setPenProperties)
        self.updateData()

    def updateData(self):
        bevel = ""
        if self.beveled:
            bevel = "<br>Beveled"
        self.label.setText("Width = {}<br>Style = {}{}".format(self.width, self.style, bevel))

    def setPenProperties(self):
        dialog = PenPropertiesDlg(self)
        dialog.widthSpinBox.setValue(self.width)
        dialog.beveledCheckBox.setChecked(self.beveled)
        dialog.styleComboBox.setCurrentIndex(
                dialog.styleComboBox.findText(self.style))
        if dialog.exec_():
            self.width = dialog.widthSpinBox.value()
            self.beveled = dialog.beveledCheckBox.isChecked()
            self.style = dialog.styleComboBox.currentText()
            self.updateData()


app = QApplication(sys.argv)
form = Form()
form.resize(400, 200)
form.show()
app.exec_()
View Code

原文地址:https://www.cnblogs.com/caiyishuai/p/13270769.html