Page 1 of 1

problem with gpio

Posted: Fri Feb 02, 2018 7:20 pm
by noiling
in python i always get
i have a raspberry 3

>>> GPIO.setmode(GPIO.BCM)

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
GPIO.setmode(GPIO.BCM)
NameError: name 'GPIO' is not defined

Re: problem with gpio

Posted: Fri Feb 02, 2018 10:02 pm
by pcmanbob
Not much actual information in your post but I am guessing you have not got the line

Code: Select all

import RPi.GPIO as GPIO
At the start of you python program

Re: problem with gpio

Posted: Sat Feb 03, 2018 4:34 am
by noiling
thanks
i did right

now ive got the problem want to save my program
and later edit it
but if i save and open it then there arn the

>>>

in the next line

Re: problem with gpio

Posted: Sat Feb 03, 2018 10:00 am
by B.Goode
The crystal ball is a little hazy this morning, but it seems likely that you are using the IDLE (or IDLE3) IDE and typing into the default Python Shell Window that appears when you start the IDE. This is nothing to do with the use of gpio.

The Python Shell Window gives you an 'immediate' mode or REPL where each line you enter is directly executed by the Python interpreter. This is not an appropriate way to create a python script that you wish to save and rerun.

From the File/New drop down menu option at the top left of the IDE create a new, blank, file which will not have the >>> prompt. Type your script there and then save it.

Re: problem with gpio

Posted: Sat Feb 03, 2018 10:08 am
by noiling
ok thanks
now ive got the problem i dont understand what exaktly to import!!! here is my skript

Code: Select all

import Tkinter
from Tkinter import*
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.Out)

def anzeige(lb):
    if GPIO.input(4):
        lb.config(text="GPIO 4 high")
    else:
        lb.config(text="GPIO 4 low")
    lb.after(100, anzeige, lb)

def an():
    GPIO.output(4, 1)

def aus():
    GPIO.output(4, 0)

root = Tk()
root.title("chris ist der beste superdaed")
Label(root, text="-----").pack()
lb = Label(root, text="an")
lb.pack()
Label(root, text="-----").pack()
Button(root, text="an", width=40, command=an).pack()
Button(root, text="aus", width=40, command=an).pack()
anzeige(lb)
mainloop()


the message i get is


import error, no modules named Tinker

Re: problem with gpio

Posted: Sat Feb 03, 2018 10:11 am
by noiling
coding seems not to be the problem because another script (i did really code on my own) that works is

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
print ("hallo")
for n in range(10):
    print (n)
    GPIO.output(4, 0)
    GPIO.output(17, 1)
    sleep(0.5)
    GPIO.output(4, 1)
    GPIO.output(17, 0)
    sleep(0.5)
print ("superd├Ąd ist der beste")
GPIO.output(4,0)

Re: problem with gpio

Posted: Sat Feb 03, 2018 10:17 am
by B.Goode
Python relies on indentation for its syntax. Posting plain text in the forum software removes indentation, making the script close to meaningless. Please use the Code tag mark-up provided by the Code button in the top row of the message edit screen. (You can retrospectively modify your existing post.)

Please post the whole of the error message, accurately. There is indeed no standard module named Tinker. It is also important to distinguish between the (different) module names needed for use with Python3 and Python respectively.

Re: problem with gpio

Posted: Sat Feb 03, 2018 11:27 am
by pcmanbob
Are you running you code using python 3 by any chance.

python 2 import Tkinter

python 3 import tkinter (note not capitals.)

Re: problem with gpio

Posted: Sat Feb 03, 2018 11:40 am
by neilgl
Probably the issue is that the module is named Tkinter in Python 2 and tkinter in Python 3.
This script a.py will fail if you run it from a terminal

Code: Select all

python3 a.py

Code: Select all

import Tkinter
from Tkinter import*
print ("hello")
ImportError: No module named 'Tkinter'
but b.py is OK

Code: Select all

import tkinter
from tkinter import*
print ("hello")

Re: problem with gpio

Posted: Sat Feb 03, 2018 12:18 pm
by PingoBags
You could try using try except, that way it should work in python 2&3

Code: Select all

try:
  import Tkinter
  from Tkinter import*
except ImportError:
  import tkinter
  from tkinter import*

Re: problem with gpio

Posted: Sat Feb 03, 2018 12:31 pm
by B.Goode
We really need to hear from the OP, @noilng, because none of these replies directly address the error that the OP reported:
the message i get is


import error, no modules named Tinker
The errors of capitalisation and plural indicate that has been transcribed inaccurately, so nothing can be inferred with confidence from the text of the error.

A cut and paste of the entire error feedback would allow us to see what version of Python is being used, and what the content of the live version of the script actually contains.