0

Хочу сделать плавное передвижение кнопки по нажатии на нее, как?

Код:

def MouseMenu():
    ui.MouseClicker.move(20, 20)

...    

ui.MouseClicker.clicked.connect(lambda: MouseMenu())
2

1 ответ 1

0

sorry, я не просил вас предоставить проект,
я попросил предоставить минимально-воспроизводимый пример.

Класс QPropertyAnimation анимирует свойства Qt.

То, что вы хотите сделать, называется анимацией и выглядит это примерно так:

import sys
from random import randrange
from PyQt5.Qt import *

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.mouseClicker = QPushButton('Click \nme', self)
        self.mouseClicker.setGeometry(350, 200, 96, 96)
        self.mouseClicker.clicked.connect(self.mouse_menu) 
        
        self.pos_x1 = self.mouseClicker.pos().x()
        self.pos_y1 = self.mouseClicker.pos().y()
        self.w, self.h = ..., ...

    def new_pos(self):
        self.pos_x1 = self.mouseClicker.pos().x()
        self.pos_y1 = self.mouseClicker.pos().y()
        
    def mouse_menu(self):
        self.animation = QPropertyAnimation(self.mouseClicker, b"geometry")
        self.animation.setDuration(2000)
        self.pos_x2 = randrange(1, self.w-100)
        self.pos_y2 = randrange(1, self.h-100)
        self.animation.setStartValue(QRect(self.pos_x1, self.pos_y1, 96, 96))
        self.animation.setEndValue(QRect(self.pos_x2, self.pos_y2, 96, 96))
        self.animation.start()
        QTimer.singleShot(2000, self.new_pos)        
 
    def resizeEvent(self, event):
        self.w, self.h = self.size().width(), self.size().height() 


QSS = '''
QPushButton {
    background-color: #2196f3;
    min-width:  96px;
    max-width:  96px;
    min-height: 96px;
    max-height: 96px;
    border-radius: 48px;        /* круглый */
}
'''
    

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyleSheet(QSS)
    ex = MainWindow()
    ex.resize(800, 500)
    ex.show()
    sys.exit(app.exec_())

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

1
  • @CoCuCka что не так?
    – S. Nick
    26 авг 2021 в 15:33

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.