2

I'm just starting with the pi and to be more clear I'm spending time working with the Raspberry Pi Workshop for Beginners series on Youtube.To write an run code we use the Python 3 IDE but I don't like it that much so instead of that I'm using Thonny.

This time we are creating a simple GUI that controls just an LED and after writing the code and running it on Thonny it doesn't work but on Python 3 it does. I guess they use different interpreters, but I want to know what is exactly happening and where can I look ant more information about. So this is the code :

It doesn't work: Running it using thonny is like if the is frozen. If we run with python3 all of the features work well like mouse over button trigger that changes the background and that stuff.

## Toggle an LED when the GUI button is pressed ##

from tkinter import *
import tkinter.font
from gpiozero import LED
import RPi.GPIO
RPi.GPIO.setmode(RPi.GPIO.BCM)

### HARDWARE DEFINITIONS ###
led=LED(14)

### GUI DEFINITIONS ###
win = Tk()
win.title("LED Toggler")
myFont = tkinter.font.Font(family = 'Helvetica', size = 12, weight = "bold")


### Event Functions ###
def ledToggle():
    if led.is_lit:
        led.off()
        ledButton["text"]="Turn LED on" # Change only the button text property
    else:
        led.on()
        ledButton["text"]="Turn LED off"

def close():
    RPi.GPIO.cleanup()
    win.destroy()



### WIDGETS ###

# Button, triggers the connected command when it is pressed
ledButton = Button(win, text='Turn LED on', font=myFont, command=ledToggle, bg='bisque2', height=1, width=24)
ledButton.grid(row=0,column=1)

exitButton = Button(win, text='Exit', font=myFont, command=close, bg='red', height=1, width=6)
exitButton.grid(row=2, column=1)

win.protocol("WM_DELETE_WINDOW", close) # cleanup GPIO when user closes window

win.mainloop() # Loops forever

Thank you!

  • Need to be more specific rather then "it doesn't work" Explain what is happening, is there errors? – Chad G Jul 9 '18 at 15:10
  • Read this announcement re scratch and Thonny. Tell us how you installed Thonny first, as I suspect that is the problem. If you've installed iaw the link above, please tell us what you mean by "doesn't work", and we'll try to help. – Seamus Jul 9 '18 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.