window_screen_pyqt.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. from PyQt5.QtWidgets import QWidget, QApplication,QMainWindow,QLabel,QTextEdit,QPushButton
  2. from PyQt5.QtGui import QPixmap,QPainter,QResizeEvent,QCloseEvent,QPaintEvent,QFont
  3. from PyQt5.QtCore import QSize,QTimer,QRect,Qt
  4. import mqtt_async as mt
  5. import message_pb2 as MSG
  6. import google.protobuf.text_format as tf
  7. from google.protobuf import json_format
  8. class DirectBtn(QPushButton):
  9. def __init__(self, parent,text,type ,action):
  10. super().__init__(text,parent=parent)
  11. self.actionCallback=action
  12. self.type=type
  13. self.show()
  14. def mouseMoveEvent(self, e):
  15. pass
  16. def mousePressEvent(self, e):
  17. self.actionCallback(self.type,"press")
  18. def mouseReleaseEvent(self, e) -> None:
  19. self.actionCallback(self.type,"stop")
  20. def enterEvent(self, a0):
  21. return super().enterEvent(a0)
  22. def leaveEvent(self, a0):
  23. #self.actionCallback(self.type,"stop")
  24. return super().leaveEvent(a0)
  25. class Frame(QMainWindow):
  26. def __init__(self):
  27. super(Frame, self).__init__()
  28. self.mtclient=mt.MqttAsync("pyTool")
  29. self.mtclient.connect("192.168.2.132",1883,"admin","zx123456")
  30. self.v=0
  31. self.vth=0
  32. self.InitUI()
  33. def InitUI(self):
  34. self.label_ip=QLabel(self)
  35. self.edit_ip=QTextEdit(self)
  36. self.label_port = QLabel(self)
  37. self.edit_port=QTextEdit(self)
  38. self.btnConnect=QPushButton(self)
  39. self.btnConnect.clicked.connect(self.ConnectAction)
  40. #self.car_number_txt.setStyleSheet("border-width:1px;border-style:solid;border-color:rgb(150,150,150)")
  41. self.label_ip.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
  42. self.label_port.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
  43. self.label_ip.setStyleSheet("color:blue")
  44. self.label_port.setStyleSheet("color:blue")
  45. self.label_ip.setText("IP:")
  46. self.edit_ip.setText("192.168.2.132")
  47. self.label_ip.setGeometry(10, 10, 50, 30)
  48. self.edit_ip.setGeometry(70, 10, 150, 30)
  49. self.label_port.setText("端口:")
  50. self.edit_port.setText("1883")
  51. self.label_port.setGeometry(10, 45, 50, 30)
  52. self.edit_port.setGeometry(70, 45, 150, 30)
  53. self.btnConnect.setGeometry(230, 10, 100, 65)
  54. self.btnConnect.setText("连接")
  55. self.label_v=QLabel(self)
  56. self.edit_v=QTextEdit(self)
  57. self.label_vth = QLabel(self)
  58. self.edit_vth=QTextEdit(self)
  59. self.btnChangeSpeed=QPushButton(self)
  60. self.btnChangeSpeed.clicked.connect(self.ChangeSpeedAction)
  61. #self.car_number_txt.setStyleSheet("border-width:1px;border-style:solid;border-color:rgb(150,150,150)")
  62. self.label_v.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
  63. self.label_vth.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
  64. self.label_v.setStyleSheet("color:blue")
  65. self.label_vth.setStyleSheet("color:blue")
  66. self.label_v.setText("线速度:")
  67. self.label_v.setGeometry(10, 210, 50, 30)
  68. self.edit_v.setGeometry(70, 210, 150, 30)
  69. self.label_vth.setText("角速度:")
  70. self.label_vth.setGeometry(10, 245, 50, 30)
  71. self.edit_vth.setGeometry(70, 245, 150, 30)
  72. self.btnChangeSpeed.setGeometry(230, 210, 100, 65)
  73. self.btnChangeSpeed.setText("变速")
  74. ###
  75. self.btnRotation=DirectBtn(self,"逆时针","LR",self.monitorAction)
  76. self.btnRotation.setGeometry(450, 150, 60, 60)
  77. self.btnRotation=DirectBtn(self,"顺时针","RR",self.monitorAction)
  78. self.btnRotation.setGeometry(600, 150, 60, 60)
  79. self.btnRotation=DirectBtn(self,"前进","UP",self.monitorAction)
  80. self.btnRotation.setGeometry(520, 150, 70, 60)
  81. self.btnRotation=DirectBtn(self,"后退","DOWN",self.monitorAction)
  82. self.btnRotation.setGeometry(520, 250, 70, 60)
  83. def ConnectAction(self):
  84. self.btnConnect.setStyleSheet("color:yellow")
  85. self.btnConnect.setText("连接中")
  86. self.btnConnect.setStyleSheet("color:green")
  87. def ChangeSpeedAction(self):
  88. self.btnChangeSpeed.setStyleSheet("color:yellow")
  89. self.btnChangeSpeed.setText("变速")
  90. self.btnChangeSpeed.setStyleSheet("color:green")
  91. def monitorAction(self,type,action):
  92. if type=="LR": #逆时针旋转
  93. if action=="press":
  94. self.v=0
  95. self.vth=0.1
  96. print("逆时针旋转")
  97. else:
  98. self.v=0
  99. self.vth=0
  100. print("停止旋转")
  101. if type=="RR": #顺时针旋转
  102. if action=="press":
  103. self.v=0
  104. self.vth=-0.1
  105. print("顺时针旋转")
  106. else:
  107. self.v=0
  108. self.vth=0
  109. print("停止旋转")
  110. if type=="UP":
  111. if action=="press":
  112. self.v=0.2
  113. self.vth=0
  114. print("前进")
  115. else:
  116. self.v=0
  117. self.vth=0
  118. print("停止前进")
  119. if type=="DOWN":
  120. if action=="press":
  121. self.v=-0.2
  122. self.vth=0
  123. print("后退")
  124. else:
  125. self.v=0
  126. self.vth=0
  127. print("停止后退")
  128. cmd=MSG.ChangeSpeedCmd()
  129. cmd.v=self.v
  130. cmd.vth=self.vth
  131. msg=json_format.MessageToJson(cmd,True)
  132. self.mtclient.publish("cmd1",msg)
  133. '''if type=="TL": #左转弯
  134. if action=="press":
  135. print("左转弯")
  136. else:
  137. print("停止左转弯")
  138. if type=="TR": #右转弯
  139. if action=="press":
  140. print("右转弯")
  141. else:
  142. print("停止右转弯")'''
  143. def closeEvent(self, a0: QCloseEvent) -> None:
  144. pass
  145. def resizeEvent(self, a0: QResizeEvent) -> None:
  146. pass
  147. '''w, h = a0.size().width(), a0.size().height()
  148. w, h = self.size().width(), self.size().height()
  149. top_w, top_h = w, int(h * 0.15)
  150. car_number_w,car_number_h=w, int(h * 0.10)
  151. txt_w, txt_h = w, int(h * 0.20)
  152. arrow_w, arrow_h = w, int(h * 0.55)
  153. font = QFont()
  154. font.setBold(True)
  155. font.setPixelSize(int(w / 40))
  156. self.measure_info_txt.setFont(font)
  157. self.measure_info_txt.setGeometry(0, 0, top_w, top_h)
  158. font.setPixelSize(int(w / 20))
  159. self.car_number_txt.setFont(font)
  160. self.car_number_txt.setGeometry(0, top_h, car_number_w, car_number_h)
  161. self.panel_txt.setGeometry(0, top_h+car_number_h, txt_w, txt_h)
  162. self.panel_arrow.setGeometry(0, top_h +car_number_h+ txt_h, arrow_w, arrow_h)'''