PyQt5 组件和容器 (w/ comments)

QWidget ->{QPushButton, QAction, QLineEdit, QMessageBox, QMainWindow]

import sys
from PyQt5.QtWidgets import QApplication, \
    QWidget, QPushButton, QAction, QLineEdit, \
    QMessageBox, QMainWindow
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class mainWindow(QMainWindow):
    #the QmainWindow has the menu bar
    def __init__(self):
        super(mainWindow, self).__init__()
        self.title = "PY"
        self.left = 100  #the coordinate of window opened on screen
        self.top = 100
        self.width = 640
        self.height = 320
        self.initUI()
        self.count = 0

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        #create the menu bar container
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu("file")
        editMenu = mainMenu.addMenu("edit")
        viewMenu = mainMenu.addMenu("view")
        searchMenu = mainMenu.addMenu("search")
        toolMenu = mainMenu.addMenu("tool")
        helpMenu = mainMenu.addMenu("help")

        exitButton = QAction("Exit", self)
        exitButton.setShortcut("ctrl+Q")
        # self.close is the standard function
        exitButton.triggered.connect(self.close)
        # unwrap menu of fileMenu
        fileMenu.addAction(exitButton)


        self.show()



class App(QWidget):
    # Qwiget: provide methods for  window configuration and initiation
    def __init__(self):
        super(App, self).__init__()
        self.title = "PY"
        self.left = 100  #the coordinate of window opened on screen
        self.top = 100
        self.width = 640
        self.height = 320
        self.initUI()
        self.count = 0

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top, self.width, self.height)
        #the first button
        button = QPushButton("example", self)
        button.setToolTip("this is an example button") #label when cursor hover
        button.move(10, 20) #the position of the corner of button at the window
        button.clicked.connect(self.on_click)   #binding the method of clicked button
        #the second button
        button2 = QPushButton("发送",self)
        button2.setToolTip("发送")
        button2.move(10, 80)
        button2.clicked.connect(self.submit)
        #the txt box
        self.textbox = QLineEdit(self)
        self.textbox.move(10, 150)
        self.textbox.resize(280,40)

        self.show()


    @pyqtSlot() #反应槽
    def on_click(self):
        print("COUNT = ", self.count)
        self.count += 1

    @pyqtSlot()
    def submit(self):
        msg = self.textbox.text()
        print(msg) # submit the text content in the TextBox Widget
        QMessageBox.question(self, "Tips", "->"+msg,
                             QMessageBox.Apply,
                             QMessageBox.Ok) # the message box (can be alert or tips to warn the user)
        self.textbox.setText("")



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    rx = mainWindow()
    sys.exit(app.exec_())

QWidget -> {QApplication, QPushButton, QDialog, QVBLayout, QGridLayout, QGroupBox}

import sys
from PyQt5.QtWidgets import QApplication, \
    QWidget, QPushButton, QAction, QLineEdit, \
    QMessageBox, QMainWindow, QHBoxLayout, QDialog, \
     QVBoxLayout, QGridLayout, QGroupBox, QFormLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class App(QDialog):
    def __init__(self):
        super(App, self).__init__()
        self.title = "QDialog"
        self.left = 10  #the coordinate of window opened on screen
        self.top = 10
        self.width = 200
        self.height = 150
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.createGridLayout()
        windowlayout = QVBoxLayout()  #QVBoxLayout create the block to slice into grid
        windowlayout.addWidget(self.horizontalGroupBox)
        # initiate the pre-defined slice grip
        self.setLayout(windowlayout)
        self.show()

    def createGridLayout(self):
        # create a group by QGroupBox
        # and slice them into grid by QgridLayout
        self.horizontalGroupBox = QGroupBox("Grid")
        layout = QGridLayout()
        layout.setColumnStretch(1, 4)
        layout.setColumnStretch(1, 4)
        button = QPushButton("Press for Test")
        button.clicked.connect(self.feedBack)
        layout.addWidget(button, 0, 0)
        layout.addWidget(QPushButton("2"), 0, 1)
        layout.addWidget(QPushButton("3"), 0, 2)
        layout.addWidget(QPushButton("4"), 1, 0)
        layout.addWidget(QPushButton("5"), 1, 1)
        layout.addWidget(QPushButton("6"), 1, 2)
        layout.addWidget(QPushButton("7"), 2, 0)
        layout.addWidget(QPushButton("8"), 2, 1)
        layout.addWidget(QPushButton("9"), 2, 2)
        self.horizontalGroupBox.setLayout(layout)

    @pyqtSlot()
    def feedBack(self):
        print("the first button works")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = App()
    sys.exit(app.exec_())

PyQt5 embedded with Matplotlib

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
import time
import matplotlib
matplotlib.use("Qt5Agg")
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(710, 350, 75, 23))
        self.pushButton.setObjectName("submit")
        self.displayData = QtWidgets.QListView(self.centralwidget)
        self.displayData.setGeometry(QtCore.QRect(20, 350, 681, 200))
        self.displayData.setObjectName("displayData")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(710, 380, 75, 23))
        self.pushButton_2.setObjectName("save")
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(710, 410, 75, 23))
        self.pushButton_3.setObjectName("clear")
        self.mainView = QtWidgets.QListView(self.centralwidget)
        self.mainView.setGeometry(QtCore.QRect(20, 10, 431, 331))
        self.mainView.setObjectName("mainView")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "submit"))
        self.pushButton_2.setText(_translate("MainWindow", "save"))
        self.pushButton_3.setText(_translate("MainWindow", "clear"))

# class testForm(QtWidgets.QMainWindow):
#     def PushButton1Clicked(self):
#             box = QtWidgets.QMessageBox()
#             box.warning(self,"提示","这是一个按钮事件")

class MyFigure(FigureCanvas):
    def __init__(self, wdith = 5, height = 4, dpi = 100):
        self.fig = Figure(figsize=(wdith, height), dpi=dpi)
        super(MyFigure, self).__init__(self.fig)
        self.axes = self.fig.add_subplot(111)
    def plotsin(self):
        self.axes0 = self.fig.add_subplot(111)
        t = np.arange(0.0, 3.0, 0.01)
        s = np.sin(2*np.pi*t)
        self.axes0.plot(t,s)

class MainDialog(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainDialog, self).__init__()
        self.setupUi(self)
        self.setMinimumSize(0,0)

        self.F = MyFigure(wdith=1, height=1, dpi=100)
        self.plotcos()
        self.gridlayout = QtWidgets.QGridLayout(self.displayData)
        self.gridlayout.addWidget(self.F, 0, 1)
        # print(MainDialog.__mro__)
        # the window will show-up automatically after initialization
        self.show()

    def plotcos(self):
        t = np.arange(0.0, 5.0, 0.01)
        s = np.cos(2 * np.pi * t)
        self.F.axes.plot(t, s)
        self.F.fig.suptitle("cos")

if __name__ == "__main__":
    # app = QtWidgets.QApplication(sys.argv)
    # form = testForm()
    # ui = Ui_MainWindow()
    # ui.setupUi(form)
    # form.show()
    # sys.exit(app.exec_())
    app = QApplication(sys.argv)
    main = MainDialog()
    sys.exit(app.exec_())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值