yes_no_button.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from distutils.version import LooseVersion
4 import os
5 import rospkg
6 import rospy
7 import rosservice
8 
9 import python_qt_binding
10 from python_qt_binding import loadUi
11 from qt_gui.plugin import Plugin
12 
13 from jsk_gui_msgs.srv import YesNo
14 from jsk_gui_msgs.srv import YesNoResponse
15 
16 # For both qt4 and qt5
17 if LooseVersion(python_qt_binding.QT_BINDING_VERSION).version[0] >= 5:
18  from python_qt_binding.QtWidgets import QWidget
19 else:
20  from python_qt_binding.QtGui import QWidget
21 
22 
23 class YesNoButtonWidget(QWidget):
24  def __init__(self):
25  super(YesNoButtonWidget, self).__init__()
26  rp = rospkg.RosPack()
27  ui_file = os.path.join(
28  rp.get_path('jsk_rqt_plugins'), 'resource', 'yes_no_button.ui')
29  loadUi(ui_file, self)
30  self.setObjectName('YesNoButtonUi')
31  self.yes_button.clicked[bool].connect(self._clicked_yes_button)
32  self.no_button.clicked[bool].connect(self._clicked_no_button)
33  self.yes_button.setEnabled(False)
34  self.no_button.setEnabled(False)
35  self.yes = None
36  service_name = rospy.get_namespace() + 'rqt_yn_btn'
37  if service_name in rosservice.get_service_list():
38  rospy.logwarn('{} is already advertised'.format(service_name))
39  return
40  self.srv = rospy.Service('rqt_yn_btn', YesNo, self._handle_yn_btn)
41 
43  """Handle events of being clicked yes button."""
44  self.yes = True
45 
46  def _clicked_no_button(self):
47  """Handle events of being clicked no button."""
48  self.yes = False
49 
50  def _handle_yn_btn(self, req):
51  """Callback function of service,
52 
53  and handle enable/disable of the buttons.
54  """
55  self.message.setText(req.message)
56  self.yes = None # initialize
57  self.yes_button.setEnabled(True)
58  self.no_button.setEnabled(True)
59  while self.yes is None: # wait for user input
60  rospy.sleep(1.)
61  self.yes_button.setEnabled(False)
62  self.no_button.setEnabled(False)
63  return YesNoResponse(yes=self.yes)
64 
65  def __del__(self):
66  self.srv.shutdown()
67 
68 
70  def __init__(self, context):
71  super(YesNoButton, self).__init__(context)
72  self.setObjectName('YesNoButton')
74  context.add_widget(self._widget)


jsk_rqt_plugins
Author(s):
autogenerated on Sat Mar 20 2021 03:03:13