How to make the button reset and change the name



  • When the buttons are pressed, the text should be changed. Or track the press. LineEdit, and she needs to clean up everything that method does. setFlat(True)

    введите сюда описание изображения

    введите сюда описание изображения

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, \
        QPushButton, QLineEdit, QRadioButton, QGridLayout, QLabel, QHBoxLayout, QVBoxLayout, QMessageBox, QFileDialog
    from PyQt5.QtWidgets import QAction, QScrollArea
    from PyQt5 import QtCore, QtGui, QtWidgets
    

    class MainWork(QMainWindow):
    def init(self):
    super().init()
    # self.setupUi(self)
    self.initUI()

    def initUI(self):
        # self.widget_fast_start
        self.resize(100, 100)
        self.widget1 = QWidget(self)
        self.setCentralWidget(self.widget1)
    
        self.push = QPushButton(self.widget1)
        self.push.setText("Здесь ваш с удалением")
        self.push.move(10, 10)
    

    if name == 'main':

    app = QApplication(sys.argv)
    ex = MainWork()
    ex.show()
    sys.exit(app.exec_())
    



  • Try this:

    import sys
    from PyQt5.Qt import *
    

    class DragButton(QPushButton):

    def mousePressEvent(self, event):
        self.__mousePressPos = None
        self.__mouseMovePos = None
        if event.button() == Qt.LeftButton:
            self.__mousePressPos = event.globalPos()
            self.__mouseMovePos = event.pos()
        elif event.button() == Qt.RightButton:  
            lineEdit = QLineEdit(self)
            lineEdit.setMinimumHeight(self.size().height())
            lineEdit.editingFinished.connect(
              lambda le=lineEdit: self.editing_finished(le))
            lineEdit.show()
            lineEdit.setFocus()
        super(DragButton, self).mousePressEvent(event)
    
    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            delta = event.pos() - self.__mouseMovePos
            newPos = self.pos() + delta
            if self.parent():
                geo = self.rect().translated(newPos)
                parentRect = self.parent().rect()
                if geo.x() < 0:
                    geo.moveLeft(0)
                elif geo.right() > parentRect.right():
                    geo.moveRight(parentRect.right())
                if geo.y() < 0:
                    geo.moveTop(0)
                elif geo.bottom() > parentRect.bottom():
                    geo.moveBottom(parentRect.bottom())
                self.move(geo.topLeft())
            else:
                self.move(newPos)
        super(DragButton, self).mouseMoveEvent(event)
        
    def editing_finished(self, le):
        self.setText(le.text())
        le.hide()
    

    class MainWork(QMainWindow):
    def init(self):
    super().init()
    self.centralWidget = QWidget()
    self.setCentralWidget(self.centralWidget)

        self.push = DragButton("Кнопка", self)
    

    if name == 'main':
    app = QApplication(sys.argv)
    w = MainWork()
    w.resize(300, 300)
    w.show()
    sys.exit(app.exec_())

    введите сюда описание изображения

    введите сюда описание изображения

    введите сюда описание изображения



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2