我尝试在i2c液晶屏上显示互联网连接是否已连接,但当我尝试运行代码时,它不会显示/显示任何内容。已尝试使用其它代码查看液晶屏是否工作正常,但正常工作。
import I2C_LCD_driver
import urllib.request
mylcd = I2C_LCD_driver.lcd()
def connect(host='http://google.com'):
try:
urllib.request.urlopen(host).read() #Python 3.x
return True
except:
return False
# test
mylcd.lcd_display_string ('connected' if connect() else 'no internet!')发布于 2020-06-27 03:33:01
最好将函数调用放在函数体和if语句之外,而不是作为参数。请检查以下内容:
import I2C_LCD_driver
import urllib.request
mylcd = I2C_LCD_driver.lcd()
def connect(host='http://google.com'):
try:
urllib.request.urlopen(host).read() #Python 3.x
return True
except:
return False
# test
if connect() :
mylcd.lcd_display_string ('connected' )
else :
mylcd.lcd_display_string ('no internet!')https://stackoverflow.com/questions/62059631
复制相似问题