1

i have a promblem with GUI Tkinter, when i click the button of my gui enviroment the arm of my robot dont response more specific call the function but steal remaning in 0 degre``es from the other my code is fine because when i click from my keyboard 'w' my arm plus for each time i click the button 15 degrees. Really i dont know if i use the right button , i am beginner in the python and i need some advice. my code:

from Tkinter import *
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(15,GPIO.OUT)
pwm = GPIO.PWM(15, 50)
pwm.start(0)
root = Tk ()
Label(root, text="ANgel").grid(row = 0)
e1 = Entry(root)
e1.grid(row = 0, column = 1)
i = 0

def Angle(angle): 
   #Calculate 
   duty = angle / 18 + 2
   GPIO.output(15,True)
   pwm.ChangeDutyCycle(duty)
   time.sleep(0.3)
   GPIO.output(15, False)
   pwm.ChangeDutyCycle(0)



def servo_up(event):
   #Servo lifts arm 
   global i
   if event.keysym == 'w' and i < 180:
     key_press = event.char
     print "Key: ", event.char
     i = i + 15
     Angle(i)
     print " ", i
  else:
     print "You are in the limit: ", i, "Degrees"

 b1 = Button(root,text = 'quit', bg = "red", command = root.quit)
 b1.grid(row = 2, column = 3)

 b= Button(root, text="Arm Up")
 b.bind("<Button-1>", servo_up)
 b.grid(row=2, column = 1)

root.bind('<w>', servo_up)

root.mainloop()

GPIO.cleanup()

When i click the button from graphical enviroment, I'm getting:

You are in the limit:  0 Degrees

when i click the w from keyboard, it work:

Key:  w
     15
Key:  w
     30
Key:  w
     45

Thanks.

2
  • It looks like that if you click the button, you are still checking if event.keysym == 'w' so it is falling through to the else:
    – John S
    Commented Mar 27, 2020 at 18:17
  • 1
    @JohnS Because of the way Stack Exchange works, please consider posting your observation as an answer so that Nikos Mitsiou can accpt it.
    – Bob Brown
    Commented Mar 27, 2020 at 18:32

1 Answer 1

1

It looks like that if you click the button, you are still checking if event.keysym == 'w' so it is falling through to the else:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.