python-GUI键盘小工具
一、tkinter GUI界面
二、实现功能
连接设备、设备上电、设备使能、键盘按键控制关节移动、配置关节移动速度和角度
三、python源码
1 #coding=utf-8 2 import msvcrt 3 import threading 4 from tkinter import * 5 from tkinter import ttk 6 import tkinter as tk 7 from DucoCobot import DucoCobot 8 import math 9 10 class Display(): 11 def __init__(self): 12 self.root = tk.Tk() 13 self.root.title("DUCO CORE KEYBOARD CONTROL") 14 self.dic_key = {"q": 1, "a": -1, "w": 1, "s": -1, "e": 1, "d": -1, 15 "u": 1, "j": -1, "i": 1, "k": -1, "o": 1, "l": -1} 16 17 #第一行 18 self.ip_label = Label(self.root, text="ip: ") 19 self.ip_demo = StringVar(value="192.168.12.111") 20 self.ip_field = Entry(self.root, textvariable=self.ip_demo) 21 self.ip_label.grid(row=0, column=0, padx=10, pady=10) 22 self.ip_field.grid(row=0, column=1, padx=10, sticky=W) 23 self.ip = Entry.get(self.ip_field) 24 # 第二行 25 self.port_label = Label(self.root, text="port: ") 26 self.port_demo = StringVar(value="7003") 27 self.port_field = Entry(self.root, textvariable=self.port_demo) 28 self.port_label.grid(row=1, column=0, padx=10, pady=5) 29 self.port_field.grid(row=1, column=1, padx=10, sticky=W) 30 self.port = Entry.get(self.port_field) 31 #第三行 32 self.speed_label = Label(self.root, text="joint speed: ") 33 self.speed_demo = StringVar(value="10") 34 self.speed_field = Entry(self.root, textvariable=self.speed_demo) 35 self.speed_label.grid(row=2, column=0, padx=10, pady=5) 36 self.speed_field.grid(row=2, column=1, padx=10, sticky=W) 37 38 #第四行 39 self.angle_label = Label(self.root, text="joint angle: ") 40 self.angle_demo = StringVar(value="5") 41 self.angle_field = Entry(self.root, textvariable=self.angle_demo) 42 self.angle_label.grid(row=3, column=0, padx=10, pady=5) 43 self.angle_field.grid(row=3, column=1, padx=10, sticky=W) 44 45 # 第五行 46 self.button_connect = Button(self.root, text="connect", command=self.connect_robot) 47 self.button_connect.grid(row=4, column=0, padx=10, pady=5) 48 self.button_poweron = Button(self.root, text="poweron", command=self.poweron) 49 self.button_poweron.grid(row=4, column=1, padx=10) 50 self.button_enable = Button(self.root, text="enable", command=self.enable) 51 self.button_enable.grid(row=4, column=2, padx=10) 52 self.button_speedj = Button(self.root, text="speedj", command=lambda: self.thread_it(self.speedj)) 53 self.button_speedj.grid(row=4, column=3, padx=10) 54 self.button_speedl = Button(self.root, text="speedl", command=lambda: self.thread_it(self.speedl)) 55 self.button_speedl.grid(row=4, column=4 ,padx=10) 56 self.button_speedj = Button(self.root, text="servoj", command=lambda: self.thread_it(self.servoj)) 57 self.button_speedj.grid(row=4, column=5, padx=10) 58 self.button_speedl = Button(self.root, text="servoj_pose", command=lambda: self.thread_it(self.servoj_pose)) 59 self.button_speedl.grid(row=4, column=6, padx=10) 60 #文本框 61 self.app = ttk.Frame() 62 self.app.grid(row=5, columnspan=8) 63 self.fram1 = LabelFrame(self.app, text='log') 64 self.fram1.grid(row=5, pady=10, padx=10) 65 self.win_output = Text(self.fram1) 66 self.win_output.grid(row=5, sticky=E + W + N + S, pady=10, padx=10) 67 self.scrollbar = Scrollbar(self.fram1, orient="vertical", command=self.win_output.yview) 68 self.scrollbar.grid(row=5, column=8, sticky=E, rowspan=80) 69 self.scrollbar.config(command=self.win_output.yview) 70 self.win_output.config(yscrollcommand=self.scrollbar.set) 71 72 self.root.mainloop() 73 74 def get_ip_port_open(self): 75 self.ip = Entry.get(self.ip_field) 76 self.port = Entry.get(self.port_field) 77 self.duco_cobot = DucoCobot(self.ip, self.port) 78 self.duco_cobot.open() 79 80 def connect_robot(self): 81 self.ip = Entry.get(self.ip_field) 82 self.port = Entry.get(self.port_field) 83 self.duco_cobot = DucoCobot(self.ip, self.port) 84 if self.duco_cobot.open() == 0: 85 self.write("{}:{} connect success".format(self.ip, self.port)) 86 87 def poweron(self): 88 self.get_ip_port_open() 89 self.duco_cobot.power_on(True) 90 state = self.duco_cobot.get_robot_state() 91 if state[0] == 5 or 6: 92 self.write("poweron success") 93 else: 94 self.write("poweron fault") 95 self.write("get_robot_state: {}".format(state)) 96 97 def enable(self): 98 self.get_ip_port_open() 99 self.duco_cobot.enable(True) 100 state = self.duco_cobot.get_robot_state() 101 if state[0] == 6: 102 self.write("enable success") 103 else: 104 self.write("enable fault") 105 self.write("get_robot_state: {}".format(state)) 106 107 def speedj(self): 108 self.get_ip_port_open() 109 self.write("please press key:") 110 self.win_output.bind("<KeyPress>", self.speedj_action) 111 112 def speedj_action(self, event): 113 try: 114 self.joints_list = [0, 0, 0, 0, 0, 0] 115 self.speed = Entry.get(self.speed_field) 116 self.input_key = event.char 117 self.write(" is pressed, speedj speed is {}".format(self.speed)) 118 self.key_index = list(self.dic_key.keys()).index(self.input_key) // 2 119 self.joints_list[self.key_index] += self.dic_key[self.input_key] * float(self.speed) 120 self.duco_cobot.speedj(self.joints_list, 0.8, 500, False) 121 except ValueError: 122 self.write( 123 "press fault, please press correct key(QA=joint1, WS=joint2,ED=joint3,UJ=joint4,IK=joint5,OL=joint6)") 124 125 def speedl(self): 126 self.get_ip_port_open() 127 self.write("please press key:") 128 self.win_output.bind("<KeyPress>", self.speedl_action) 129 130 def speedl_action(self, event): 131 try: 132 self.joints_list = [0, 0, 0, 0, 0, 0] 133 self.speed = Entry.get(self.speed_field) 134 self.input_key = event.char 135 self.write(" is pressed, speedl speed is {}".format(self.speed)) 136 self.key_index = list(self.dic_key.keys()).index(self.input_key) // 2 137 self.joints_list[self.key_index] += self.dic_key[self.input_key] * float(self.speed) 138 self.duco_cobot.speedl(self.joints_list, 0.2, 500, False) 139 except ValueError: 140 self.write( 141 "press fault, please press correct key(QA=X, WS=Y,ED=Z,UJ=RX,IK=RY,OL=RZ)") 142 143 def servoj(self): 144 self.get_ip_port_open() 145 self.write("please press key:") 146 self.win_output.bind("<KeyPress>", self.servoj_action) 147 148 def servoj_action(self, event): 149 try: 150 position = self.duco_cobot.get_actual_joints_position() 151 self.angle = Entry.get(self.angle_field) 152 self.input_key = event.char 153 self.write(" is pressed, servoj angle is {}".format(self.angle)) 154 self.key_index = list(self.dic_key.keys()).index(self.input_key) // 2 155 position[self.key_index] += self.dic_key[self.input_key] * math.radians(float(self.angle)) 156 self.duco_cobot.servoj(position, 0.2, 0.4, False, 200, 25) 157 except ValueError: 158 self.write( 159 "press fault, please press correct key(QA=joint1, WS=joint2,ED=joint3,UJ=joint4,IK=joint5,OL=joint6)") 160 161 def servoj_pose(self): 162 self.get_ip_port_open() 163 self.write("please press key:") 164 self.win_output.bind("<KeyPress>", self.servoj_pose_action) 165 166 def servoj_pose_action(self, event): 167 try: 168 tcp_position = self.duco_cobot.get_tcp_pose() 169 joints_position = self.duco_cobot.get_actual_joints_position() 170 self.angle = Entry.get(self.angle_field) 171 self.input_key = event.char 172 self.write(" is pressed, servoj_pose angle is {}".format(self.angle)) 173 self.key_index = list(self.dic_key.keys()).index(self.input_key) // 2 174 if self.key_index > 2: 175 tcp_position[self.key_index] += self.dic_key[self.input_key] * math.radians(float(self.angle)) 176 else: 177 tcp_position[self.key_index] += self.dic_key[self.input_key] * float(self.angle) / 1000 178 self.duco_cobot.servoj_pose(tcp_position, 0.2, 0.4, joints_position, '', '', False, 200, 25) 179 except ValueError: 180 self.write( 181 "press fault, please press correct key(QA=X, WS=Y,ED=Z,UJ=RX,IK=RY,OL=RZ)") 182 183 184 def thread_it(self, func): 185 t = threading.Thread(target=func) 186 t.setDaemon(True) 187 t.start() 188 189 def write(self, txt): 190 self.win_output.insert(END, str(txt+"\n")) 191 192 if __name__ == '__main__': 193 Display()
四、程序打包
生成open.exe 文件,将其发送给未安装python的人,点击即可使用。
如果一个人不懂得照顾自己的情绪,不懂得表达自己的情绪的话,要么压抑生病,要么突然暴怒、欺负别人,因为这些情绪都没有消失,而是在生命中不断的累积。