我是一名初學者程序員,所以我沒有獲得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()
你可以包含你得到的確切的錯誤信息嗎?我可以在RPi.GPIO wiki上看到一條錯誤消息,看起來可能是您在此處描述的內容:http://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/,但不知道這是你真的看到了什麼。 –
@ErikJohnson你好,確切的錯誤信息是這樣的; 「異常在Tkinter的回調 回溯(最近通話最後一個): 文件 '用戶/ lib目錄/ python3.2/Tkinter的/ __ init__.py',行1426,在__call__ 回報self.func(*參數) 文件'home.pi.tkinterproject.py',run_code中的第40行 GPIO.setup(GPIO.OUT) RuntimeErorr:無法訪問/ dev/mem。試試以root身份運行!「 –
你說你以'root'運行時會出現同樣的錯誤,你能向我們展示你用來作爲'root'用戶運行你的代碼的確切命令嗎? – larsks