简体   繁体   English

i2c LCD 连接到互联网

[英]i2c LCD connected to the internet

i am trying to display internet connection if it is connected or not in the i2c lcd but when i try to run the code it will not show/display anything.我正在尝试在 i2c lcd 中显示 Internet 连接是否已连接,但是当我尝试运行代码时,它不会显示/显示任何内容。 Tried other codes to see if my lcd is working but it does.尝试了其他代码以查看我的液晶显示器是否正常工作,但确实可以。

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!')

It is better to place the function call outside the function body and if statement not as an argument.最好将 function 调用放在 function 主体之外,并且不要将if语句作为参数。 Check this:检查这个:

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!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM