为什么我的按钮不能在tkinter中调用我的函数?

时间:2019-06-24 03:55:48

标签: python sockets tkinter

我正在尝试为Tello无人机构建自己的飞行应用程序,但是当我按下按钮来移动无人机时,它不起作用。我想让Tkinter按钮调用函数send(“ forward” + str(50))。

我尝试过用不同的方法制作按钮,但仍然无法正常工作。

# Import the necessary modules
import threading 
import socket
import time 
import tkinter

# IP and port of Tello
tello_address = ('192.168.10.1', 8889)

# IP and port of local computer
phone = ''
port = 9000
localaddress = (phone,port)

# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind to the local address and port
sock.bind(localaddress)


# Send the message to Tello
def send(message):
    try:
        sock.sendto(message.encode(), tello_address)
        print("The following message is being sent:" + message)
    except Exception as e:
        print("Message Error. Tello says:" + str(e) + " Mission Failled. 
We'll get then next time")
    time.sleep(0)

# Receive the message from Tello
def recv():
    # Continuously loop and listen for incoming messages
    while True:
        # Try to receive the message. If there is a problem print the 
exception.
        try:
            response, drone = sock.recvfrom(1518)
            print("Drone says: " + response.decode(encoding="utf-8"))
        except Exception as e:
            sock.close()
            print ("Error receiving, Drone says: " + str(e))
            break

# Create and start a listening thread that runs in the background
# This utilizes our receive functions and will continuously monitor for 
incoming
recvThread = threading.Thread(target=recv)
recvThread.daemon = True
recvThread.start()

# Give your drone commands by placing code below.

top = tkinter.Tk()
B0 = tkinter.Button(top, text ="^", command = send("forward " + str(50)))
B1 = tkinter.Button(top, text ="v", command = send("back " + str(50)))

B0.pack()
B1.pack()
top.mainloop()

# Report mission status
print("Mission Succesful!")

#Clean-up your open connections by closing them.                 
so

当我运行该程序时,两个功能自动运行一次,然后什么也没有发生。我没有用过Tkinter 之前,我将不胜感激。

0 个答案:

没有答案