Topic: correzione script snipping tool  (Letto 140 volte)

0 Utenti e 1 Visitatore stanno visualizzando questo topic.

Offline Brown Oak

  • python unicellularis
  • *
  • Post: 1
  • Punti reputazione: 0
    • Mostra profilo
correzione script snipping tool
« il: Gennaio 17, 2023, 03:16 »
Buongiorno a tutti, mi sto approcciando alla programazione da pochi giorni.
Per esercizio sto creando una funzione di ritaglio di un immagine (ho messo insieme diversi script open source)
Ho però un problema che non riesco a risolvere.
dopo che è stata ritagliata l'immagine vorrei che fosse inserita nella label della finestra principale e che la finestra principale si riaprisse.
ho scritto queste righe:
       window.img_label.setPixmap(pixmap)
        self.close()
        window.show()
Ma il risultato è un macello
Come devo fare?


ecco le script:
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.begin = QtCore.QPoint()
        self.end = QtCore.QPoint()
        self.show()
       
    def initUI(self):
        self.setWindowTitle('My App')
        self.setGeometry(300, 300, 600, 400)
        # Add exit button
        exitAct = QAction(QIcon('c:\\Users\\Luke\\Desktop\\Fontsof\\exit.png'), 'Exit', self)
        exitAct.setShortcut('Ctrl+Q')
        exitAct.triggered.connect(self.close)
        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAct)
        # Add snipping tool button
        snipping_tool_button = QPushButton("", self)
        snipping_tool_button.setGeometry (200,150,100,30)
        snipping_tool_icon = QIcon('c:\\Users\\Luke\\Desktop\\Fontsof\\Snip.png')
        snipping_tool_button.setIconSize(QSize(32,32))
        snipping_tool_button.setIcon(snipping_tool_icon)
        snipping_tool_button.setStyleSheet("background-color: rgb(156, 232, 115);")
        snipping_tool_button.setFlat(True)
        snipping_tool_button.setShortcut('Ctrl+N')
        self.toolbar.addWidget(snipping_tool_button)
        snipping_tool_button.clicked.connect(self.snipping_tool_clicked)
       # Add a label
        self.img_label = QLabel(self)
        self.img_label.setGeometry(0, 44, 600, 400)
        #self.img_label.setStyleSheet('background-color: white;')
   
   
   
    # crea una finestra opacizzata
    def snipping_tool_clicked(self):
        root = tk.Tk()
        screen_width = root.winfo_screenwidth()
        screen_height = root.winfo_screenheight()
        self.setGeometry(0, 0, screen_width, screen_height)
        self.setWindowTitle(' ')
        self.begin = QtCore.QPoint()
        self.end = QtCore.QPoint()
        self.setWindowOpacity(0.3)
        QtWidgets.QApplication.setOverrideCursor(
            QtGui.QCursor(QtCore.Qt.CrossCursor)
        )
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        print('Screenshot eseguito')
        self.show()
         
    def paintEvent(self, event):
        qp = QtGui.QPainter(self)
        qp.setPen(QtGui.QPen(QtGui.QColor('black'), 3))
        qp.setBrush(QtGui.QColor(128, 128, 255, 128))
        qp.drawRect(QtCore.QRect(self.begin, self.end))

    def mousePressEvent(self, event):
        self.begin = event.pos()
        self.end = self.begin
        self.update()

    def mouseMoveEvent(self, event):
        self.end = event.pos()
        self.update()

    def mouseReleaseEvent(self, event):
        x1 = min(self.begin.x(), self.end.x())
        y1 = min(self.begin.y(), self.end.y())
        x2 = max(self.begin.x(), self.end.x())
        y2 = max(self.begin.y(), self.end.y())
        img = ImageGrab.grab(bbox=(x1, y1, x2, y2))
        img.save('capture.png')
        img = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2RGB)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        qImg = QImage(img.data, img.shape[1], img.shape[0], QImage.Format_RGB888)
        pixmap = QPixmap.fromImage(qImg)
        window.img_label.setPixmap(pixmap)
        self.close()
        window.show()
     

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    app.aboutToQuit.connect(app.deleteLater)
    sys.exit(app.exec_())
« Ultima modifica: Gennaio 17, 2023, 03:52 da Brown Oak »

Offline GlennHK

  • python sapiens sapiens
  • ******
  • Post: 1.720
  • Punti reputazione: 1
    • Mostra profilo
    • La Tana di GlennHK
Re:correzione script snipping tool
« Risposta #1 il: Gennaio 18, 2023, 09:42 »
Abbi pazienza, posti codice messo insieme alla bell'e meglio, non formattato, descrivi il problema come "il risultato è un macello" e cosa ti aspetti che ti risponda qualcuno?