Python Tkinter- GPIO引脚功能不起作用

我是一名初学者程序员,所以我没有获得Python的大量经验。我创建了一个使用树莓派记录水位的超声波传感器系统。我的程序在控制台中工作正常,但是我想为它做一个GUI,使它更具吸引力,使用Tkinter。我之前从未使用过Tkinter,所以我不确定自己做错了什么。我做了一个按钮,应该开始正在进行的实际阅读,但是每次运行时都会收到一个错误,告诉我我无法访问GPIO,并且应该尝试以root用户身份运行 - 尽管当我这样做时,同样的错误出现。Python Tkinter- GPIO引脚功能不起作用

有没有人有任何想法,我错了或它的任何其他方式通过GUI运行?我非常感谢任何帮助,因为我已经在这个问题上停留了两个多月,非常感谢!

我得到的错误信息是这个;

"Exception in Tkinter callback 

Traceback (most recent call last):

File 'user/lib/python3.2/tkinter/__init__.py', line 1426, in __call__

return self.func(*args)

File 'home.pi.tkinterproject.py', line 40 in run_code

GPIO.setup(GPIO.OUT)

RuntimeErorr: No access to /dev/mem. Try running as root!"

这是代码:

from tkinter import * 

import time

import datetime

import RPi.GPIO as GPIO

GPIO.setwarnings(False)

class Window(Frame):

def __init__(self, master = None):

Frame.__init__(self, master)

self.master = master

self.init_window()

def init_window(self):

self.master.title("GUI")

self.pack(fill=BOTH, expand=1)

quitButton = Button(self, text = "Quit", command = self.exit_window)

quitButton.place(x = 330,y = 260)

runButton = Button(self, text = "Run", command = self.run_code)

runButton.place(x = 0, y = 0)

def exit_window(self):

exit()

def run_code(self):

#set pins according to BCM GPIO references

GPIO.setmode(GPIO.BCM)

#set GPIO pins

TRIG = 23

ECHO = 24

#sets trigger to send signal, echo to recieve the signal back

GPIO.setup(TRIG,GPIO.OUT)

GPIO.setup(ECHO,GPIO.IN)

#sets output to low

GPIO.output(TRIG,False)

myLabell = Label(text = 'Initiating measurement').pack()

print ("Initiating measurement..\n")

#gives sensor time to settle for one second

time.sleep(1)

distance = averageReading()

round(distance, 2)

print ("Distance:", distance, "cm\n")

print ("Saving your measurement to file..")

ts = time.time()

timestamp = datetime.datetime.fromtimestamp(ts).strftime(' %H: %M: %S %d-%m-%Y')

textFile = open("sensorReadings" , "a")

textFile.write(str(distance)+ "cm recorded at: ")

textFile.write(str(timestamp)+ "\n")

textFile.close()

#resets pins for next time

GPIO.cleanup()

global averageReading

def averageReading():

readingOne = measure()

time.sleep(0.1)

readingTwo = measure()

time.sleep(0.1)

readingThree = measure()

reading = readingOne + readingTwo + readingThree

reading = reading/3

return reading

global measure

def measure():

global measure

#sends out the pulse to the trigger

GPIO.output(TRIG, True)

#short as possible

time.sleep(0.00001)

GPIO.output(TRIG,False)

while GPIO.input(ECHO) == 0:

pulse_start = time.time()

while GPIO.input(ECHO) == 1:

pulse_end = time.time()

pulse_duration = pulse_end - pulse_start

#half the speed of sound in cm/s

distance = pulse_duration * 34300

distance = distance/2

#python function that rounds measurement to two digits

round(distance, 2)

return distance

myGUI = Tk()

myGUI.geometry("400x300")

app = Window(myGUI)

myGUI.mainloop()

回答:

的第一个错误:

RuntimeErorr: No access to /dev/mem. Try running as root!" 

手段正是它说:你需要以适当的访问运行代码为root到GPIO子系统。当作为root运行,你会得到一个不同的错误:

NameError: global name 'averageReading' is not defined 

这是在你的代码错误发生的原因。首先,您似乎同时使用了一个全局变量。删除这一行:

global averageReading 

而且也:

global measure 

global语句是创建全局变量,只有一个功能块内使用时才有意义。

您发布的代码中存在大量格式问题(在多行中缺少缩进),并且很难判断这仅仅是复制/粘贴问题还是代码实际上不正确。

请尝试解决您的问题中的任何格式问题,以便它符合您的实际代码。

此外,ECHOTRIG用于measure函数,但从那里不可见,所以你需要解决这个问题。

以上是 Python Tkinter- GPIO引脚功能不起作用 的全部内容, 来源链接: utcz.com/qa/264202.html

回到顶部