PyQtGraph: evento de liberación del mouse para gráfico de barras

En este artículo veremos cómo podemos crear un evento de liberación del mouse con el gráfico de barras en el módulo PyQtGraph. PyQtGraph es una biblioteca de interfaz de usuario y gráficos para Python que proporciona la funcionalidad comúnmente requerida en el diseño y las aplicaciones científicas. Sus objetivos principales son proporcionar gráficos rápidos e interactivos para mostrar datos (gráficos, videos, etc.) y, en segundo lugar, proporcionar herramientas para ayudar en el desarrollo rápido de aplicaciones (por ejemplo, árboles de propiedades como los que se usan en Qt Designer). Un gráfico de barras o gráfico de barras es una tabla o gráfico que presenta datos categóricos con barras rectangulares con alturas o longitudes proporcionales a los valores que representan. Las barras se pueden trazar vertical u horizontalmente. Un gráfico de barras verticales a veces se denomina gráfico de columnas. Este evento se activa cuando se suelta el botón del mouse en el gráfico de barras.
Podemos crear una ventana de trazado y un gráfico de barras con la ayuda de los comandos que se indican a continuación. 

# creating a pyqtgraph plot window
window = pg.plot()

# creating a bar graph of green color
bargraph = pg.BarGraphItem(x=x, height=y1, width=0.6, brush='g')

Para hacer esto, hemos modificado la clase de gráfico de barras, a continuación se muestra la clase que se puede usar  

# Bar Graph class
class BarGraphItem(pg.BarGraphItem):
    
    # constructor which inherit original 
    # BarGraphItem
    def __init__(self, *args, **kwargs):
        pg.BarGraphItem.__init__(self, *args, **kwargs)

    # creating a mouse release event
    def mouseReleaseEvent(self, event):

        # decreasing the scale of the bar graph
        self.scale(0.5, 0.5)
        
        # printing the message
        print("Mouse Release Event")

A continuación se muestra la implementación. 

Python3

# importing Qt widgets
from PyQt5.QtWidgets import * import sys
 
# importing pyqtgraph as pg
import pyqtgraph as pg
from PyQt5.QtGui import *
 
# Bar Graph class
class BarGraphItem(pg.BarGraphItem):
 
    # constructor which inherit original
    # BarGraphItem
    def __init__(self, *args, **kwargs):
        pg.BarGraphItem.__init__(self, *args, **kwargs)
 
    # creating mouse press event
    def mousePressEvent(self, event):
 
        # increasing scale of bar graph
        self.scale(2, 2)
 
        # print the message
        print("Mouse Press Event")
 
    # creating a mouse release event
    def mouseReleaseEvent(self, event):
 
        # decreasing the scale of the bar graph
        self.scale(0.5, 0.5)
 
        # printing the message
        print("Mouse Release Event")
 
 
 
 
 
 
 
class Window(QMainWindow):
 
    def __init__(self):
        super().__init__()
 
        # setting title
        self.setWindowTitle("PyQtGraph")
 
        # setting geometry
        self.setGeometry(100, 100, 600, 500)
 
 
        # icon
        icon = QIcon("skin.png")
 
        # setting icon to the window
        self.setWindowIcon(icon)
 
        # calling method
        self.UiComponents()
 
        # showing all the widgets
        self.show()
 
    # method for components
    def UiComponents(self):
 
        # creating a widget object
        widget = QWidget()
 
        # creating a plot window
        plot = pg.plot()
 
        # create list for y-axis
        y1 = [5, 5, 7, 10, 3, 8, 9, 1, 6, 2]
 
        # create horizontal list i.e x-axis
        x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
        # create pyqt5graph bar graph item
        # with width = 0.6
        # with bar colors = green
        bargraph = BarGraphItem(x = x, height = y1, width = 0.6, brush ='g')
 
        # bargraph.viewRangeChanged.connect(lambda: print("sss"))
        # add item to plot window
        # adding bargraph item to the plot window
        plot.addItem(bargraph)
 
        # Creating a grid layout
        layout = QGridLayout()
 
        # setting this layout to the widget
        widget.setLayout(layout)
 
        # plot window goes on right side, spanning 3 rows
        layout.addWidget(plot, 0, 1, 3, 1)
 
        # setting this widget as central widget of the main window
        self.setCentralWidget(widget)
 
 
# create pyqt5 app
App = QApplication(sys.argv)
 
# create the instance of our Window
window = Window()
 
# start the app
sys.exit(App.exec())

Producción :

Mouse Press Event
Mouse Release Event
Mouse Press Event
Mouse Release Event
Mouse Press Event
Mouse Release Event
Mouse Press Event
Mouse Release Event

Publicación traducida automáticamente

Artículo escrito por rakshitarora y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *