Нажимаешь на кнопку виджета QPushButton и пока не отпускаешь ее выполняется действие
Хочу знать, как реализовать следующий алгоритм, используя язык Python и PyQt5 и в нем виджет QPushButton:
В окне программы при клике на кнопку определенное действие выполняется однократно (например вал повернется на определенный угол), однако если нажать на эту кнопку и удерживать зажатым левую клавишу мыши действие будет выполняться (вал будет крутиться) пока я её не отпущу.
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
Form, Window = uic.loadUiType("template1.ui")
app = QApplication([])
window = Window()
form = Form()
form.setupUi(window)
window.show()
def was_clicked_forward():
print("Clicked!")
form.pushButton.clicked.connect(was_clicked_forward)
app.exec()
template1.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>969</width>
<height>734</height>
</rect>
</property>
<property name="windowTitle">
<string>Deqart</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>420</x>
<y>100</y>
<width>125</width>
<height>160</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>▲</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>420</x>
<y>400</y>
<width>125</width>
<height>160</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>▼</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>200</x>
<y>270</y>
<width>160</width>
<height>125</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>◀</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_4">
<property name="geometry">
<rect>
<x>600</x>
<y>270</y>
<width>160</width>
<height>125</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>▶</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>360</x>
<y>310</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OX</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>550</x>
<y>310</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OX</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>460</x>
<y>350</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OY</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>460</x>
<y>260</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OY</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>969</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>Файл</string>
</property>
<addaction name="action"/>
</widget>
<widget class="QMenu" name="menu">
<property name="title">
<string>Настройки</string>
</property>
<addaction name="action_2"/>
</widget>
<widget class="QMenu" name="menu_2">
<property name="title">
<string>Программирование</string>
</property>
</widget>
<widget class="QMenu" name="menu_3">
<property name="title">
<string>Помощь</string>
</property>
</widget>
<addaction name="menuFile"/>
<addaction name="menu"/>
<addaction name="menu_2"/>
<addaction name="menu_3"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="action">
<property name="text">
<string>Выход</string>
</property>
</action>
<action name="action_2">
<property name="text">
<string>Настройки</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
Ответы (1 шт):
Автор решения: Владимир
→ Ссылка
Этот прототип вполне подойдет для дальнейшей разработки. Действие написано для кнопки "Вверх". Пора открывать ветку PyQt5 + Arduino + Raspberry Pi
main.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class New(QMainWindow):
def __init__(self):
super(New, self).__init__()
loadUi("template1.ui", self)
self.setWindowTitle('New')
self.action.triggered.connect(self.exit_action)
# Свойство автоповтора отключено по умолчанию, поэтому включаем его
self.pushButton.setAutoRepeat(True)
# Установка интервала 500 миллисекунд
self.pushButton.setAutoRepeatInterval(500)
# Вводим нужное число для начала отсчета. Я использовал 0
self.counter = 0
# Добавляем действие к кнопке
self.pushButton.clicked.connect(lambda: self.upClicked())
def upClicked(self):
self.label_4.setText(str(self.counter))
self.counter += 1
def exit_action(self):
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = New()
window.show()
sys.exit(app.exec_())
template1.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>969</width>
<height>734</height>
</rect>
</property>
<property name="windowTitle">
<string>Deqart</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>420</x>
<y>100</y>
<width>125</width>
<height>160</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>▲</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>420</x>
<y>400</y>
<width>125</width>
<height>160</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>▼</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_3">
<property name="geometry">
<rect>
<x>200</x>
<y>270</y>
<width>160</width>
<height>125</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>◀</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_4">
<property name="geometry">
<rect>
<x>600</x>
<y>270</y>
<width>160</width>
<height>125</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">font: 75 72pt "MS Shell Dlg 2";</string>
</property>
<property name="text">
<string>▶</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>360</x>
<y>310</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OX</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>550</x>
<y>310</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OX</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>460</x>
<y>350</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OY</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>460</x>
<y>260</y>
<width>50</width>
<height>50</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>25</pointsize>
</font>
</property>
<property name="text">
<string>OY</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>969</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>Файл</string>
</property>
<addaction name="action"/>
</widget>
<widget class="QMenu" name="menu">
<property name="title">
<string>Настройки</string>
</property>
<addaction name="action_2"/>
</widget>
<widget class="QMenu" name="menu_2">
<property name="title">
<string>Программирование</string>
</property>
</widget>
<widget class="QMenu" name="menu_3">
<property name="title">
<string>Помощь</string>
</property>
</widget>
<addaction name="menuFile"/>
<addaction name="menu"/>
<addaction name="menu_2"/>
<addaction name="menu_3"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="action">
<property name="text">
<string>Выход</string>
</property>
</action>
<action name="action_2">
<property name="text">
<string>Настройки</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>