我只是在学习Python而我无法弄清楚如何解决错误。下面的Python代码在Raspberry Pi 16x2屏幕上写了一些消息。在第3组消息#AVG SPEED,如果我使用打印print avgspeed
它可以正常工作,但当我将其输出到屏幕时,我收到此错误AttributeError: 'float' object has no attribute 'center'
import RPi.GPIO as GPIO
import time
from time import sleep
import socket
import fcntl
import struct
from pycgminer import CgminerAPI
LCD_RS = 25
LCD_E = 24
LCD_D4 = 23
LCD_D5 = 17
LCD_D6 = 18
LCD_D7 = 22
LCD_WIDTH = 16
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80
LCD_LINE_2 = 0xC0
E_PULSE = 0.00005
E_DELAY = 0.00005
def main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(LCD_E, GPIO.OUT)
GPIO.setup(LCD_RS, GPIO.OUT)
GPIO.setup(LCD_D4, GPIO.OUT)
GPIO.setup(LCD_D5, GPIO.OUT)
GPIO.setup(LCD_D6, GPIO.OUT)
GPIO.setup(LCD_D7, GPIO.OUT)
lcd_init()
#UNIT
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string("UNIT 1",2)
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string("MODEL BETA",2)
sleep(5)
#CLEAR
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string("",2)
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string("",2)
sleep(0.4)
#IP ADDRESS
pi_ip = get_ip_address('eth0')
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string("IP Address",2)
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string(pi_ip,2)
sleep(5)
#CLEAR
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string("",2)
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string("",2)
sleep(0.4)
# AVG SPEED
cgminer = CgminerAPI()
summary = cgminer.summary()
avgspeed = avg_speed()
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string("Avg. Speed",2)
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string(avgspeed,2)
sleep(5)
#CLEAR
lcd_byte(LCD_LINE_1, LCD_CMD)
lcd_string("",2)
lcd_byte(LCD_LINE_2, LCD_CMD)
lcd_string("",2)
sleep(0.4)
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def avg_speed():
cgminer = CgminerAPI()
summary = cgminer.summary()
avg_speed_value = cgminer.command('summary')['SUMMARY'][0]['MHS av']
return avg_speed_value
def lcd_init():
lcd_byte(0x33,LCD_CMD)
lcd_byte(0x32,LCD_CMD)
lcd_byte(0x28,LCD_CMD)
lcd_byte(0x0C,LCD_CMD)
lcd_byte(0x06,LCD_CMD)
lcd_byte(0x01,LCD_CMD)
def lcd_string(message,style):
if style==1:
message = message.ljust(LCD_WIDTH," ")
elif style==2:
message = message.center(LCD_WIDTH," ")
elif style==3:
message = message.rjust(LCD_WIDTH," ")
for i in range(LCD_WIDTH):
lcd_byte(ord(message[i]),LCD_CHR)
def lcd_byte(bits, mode):
GPIO.output(LCD_RS, mode)
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x10==0x10:
GPIO.output(LCD_D4, True)
if bits&0x20==0x20:
GPIO.output(LCD_D5, True)
if bits&0x40==0x40:
GPIO.output(LCD_D6, True)
if bits&0x80==0x80:
GPIO.output(LCD_D7, True)
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)
GPIO.output(LCD_D4, False)
GPIO.output(LCD_D5, False)
GPIO.output(LCD_D6, False)
GPIO.output(LCD_D7, False)
if bits&0x01==0x01:
GPIO.output(LCD_D4, True)
if bits&0x02==0x02:
GPIO.output(LCD_D5, True)
if bits&0x04==0x04:
GPIO.output(LCD_D6, True)
if bits&0x08==0x08:
GPIO.output(LCD_D7, True)
time.sleep(E_DELAY)
GPIO.output(LCD_E, True)
time.sleep(E_PULSE)
GPIO.output(LCD_E, False)
time.sleep(E_DELAY)
if __name__ == '__main__':
main()
回溯:
Traceback (most recent call last):
File "screen.py", line 165, in <module>
main()
File "screen.py", line 81, in main
lcd_string(avgspeed,2)
File "screen.py", line 117, in lcd_string
message = message.center(LCD_WIDTH," ")
AttributeError: 'float' object has no attribute 'center'
有人可以帮我这个吗?
由于
答案 0 :(得分:4)
显然,在某些时候,你正在使用不是字符串而是浮点数来调用lcd_string
。所以,当它这样做时:
message = message.center(LCD_WIDTH," ")
...失败,因为float
个对象没有center
方法。
center
案例中失败的事实意味着它必须是您调用lcd_string(<something>, 2)
的地方之一。我的第一个猜测是lcd_string(avgspeed,2)
,因为avgspeed
确实听起来像是浮点而不是字符串。
(现在你已经发布了追溯,它直接告诉你我必须猜到的。失败的线正是我说的那条线,调用它的线就是我怀疑的线这就是为什么你应该总是查看回溯,并将它们粘贴到你的SO问题 - 否则,你必须扫描代码并猜测。)
如果用print message
替换它似乎有效的原因是print
命令自动将其参数字符串化,所以即使你传递float
,它也会打印出来那个float
。
如果您希望lcd_string
功能以相同的方式工作,您可以通过在其开头添加一行来轻松实现:
message = str(message)
或者,将代码更改为永远不会使用其他任何内容调用lcd_string
。所以:
lcd_string(str(avgspeed), 2)
...或者,如果你想要更多地控制格式化,可以这样:
lcd_string(format(avgspeed, '.2f'), 2)
由于任何开发人员都有时会把它搞砸(至少我会,你显然也会这样),并且你很难自己调试那些搞砸了,你可能想添加一个在函数顶部的行,为您提供更好的错误消息,如:
assert isinstance(message, str)
答案 1 :(得分:0)