Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter有问题:没有显示名称错误_Python - Fatal编程技术网

Python Tkinter有问题:没有显示名称错误

Python Tkinter有问题:没有显示名称错误,python,Python,我无法运行python程序,因为我遇到了此错误。我在我的Raspberry Pi 2上使用Raspbian操作系统,我正在制作一个小“气象站”。我的python版本是2.7.16。但当我尝试使用Python3.6时,它完全崩溃了。我不能用pip3下载任何软件包。我只能在python3上运行非常简单的程序,比如“Hello World” Error: _tkinter.TclError: no display name and no $DISPLAY environment variable

我无法运行python程序,因为我遇到了此错误。我在我的Raspberry Pi 2上使用Raspbian操作系统,我正在制作一个小“气象站”。我的python版本是2.7.16。但当我尝试使用Python3.6时,它完全崩溃了。我不能用pip3下载任何软件包。我只能在python3上运行非常简单的程序,比如“Hello World”

Error:    _tkinter.TclError: no display name and no $DISPLAY environment variable
我该怎么办?

我的代码:

# -*- coding: utf-8 -*-

from Tkinter import *
import Tkinter as tk
import Adafruit_DHT as dht
import threading
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.OUT)
GPIO.output(40, GPIO.HIGH)

root = tk.Tk()

image = PhotoImage(file="background.gif")

background=Label(root, image=image)
background.place(x=0,y=0,relwidth=1, relheight=1)

temperature = StringVar()
temperature.set("----"+" °C")
humidity = StringVar()
humidity.set("----"+" %")
temperatureLabel = Label(root, fg="white", background="#00dbde", textvariable=temperature, font=("Helvetica$
temperatureLabel.place(x=580, y=100)

humidityLabel = Label(root, fg="white", background="#00dbde", textvariable=humidity, font=("Helvetica", 40,$
humidityLabel.place(x=580, y=200)
root.attributes("-fullscreen",True)
root.bind("<1>",exit)

def exit():
        root.quit()

def readSensor():
        root.after(2000, readSensor)
        h,t = dht.read_retry(dht.DHT22,20)
        temp = "%.1f" %t
        temperature.set(temp+" °C")
        hum = "%.1f" %h
        humidity.set(hum+"  %")

root.after(2000, readSensor)

root.mainloop()
#-*-编码:utf-8-*-
从Tkinter进口*
将Tkinter作为tk导入
进口AdafruitDHT作为DHT
导入线程
将RPi.GPIO导入为GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40,GPIO.OUT)
GPIO.输出(40,GPIO.高)
root=tk.tk()
image=PhotoImage(file=“background.gif”)
背景=标签(根,图像=图像)
background.place(x=0,y=0,relwidth=1,relheight=1)
温度=StringVar()
温度设置(“----”+“°C”)
湿度=StringVar()
湿度设置(“---”+“%”)
temperatureLabel=标签(根,fg=“白色”,background=“#00dbde”,textvariable=温度,font=(“Helvetica$
温度标签位置(x=580,y=100)
humidityLabel=标签(根,fg=“白色”,background=“#00dbde”,textvariable=湿度,font=(“Helvetica”,40$
湿度标签位置(x=580,y=200)
属性(“-fullscreen”,True)
root.bind(“,退出)
def exit():
root.quit()
def读取传感器():
root.after(2000,读取传感器)
h、 t=dht.read\u retry(dht.DHT22,20)
temp=“%1f”%t
温度设定值(温度+“°C”)
hum=“.1f”%h
湿度设置(嗡嗡声+“%”)
root.after(2000,读取传感器)
root.mainloop()

Tkinter不是从pip安装的。请尝试在终端中运行此选项:

sudo apt install python3-tk
对于python2使用:

sudo apt install python-tk

请提供完整的错误输出,以及。是否所有程序都会导致该错误消息?“我应该怎么做?”-您应该做的第一件事是搜索该网站以获得确切的错误消息。如果我使用python2,我会出现此错误。为什么?我应该怎么做?