So basically I have a tkinter app. I have three buttons: one shows the temperature, one shows humidity and one shows pressure. The thing that I am aiming for is that everytime one of the buttons is clicked is erases the current display (e.g. it is displaying temperature and I want to display humidity). I tried that with the function:
所以基本上我有一个tkinter应用程序。我有三个按钮:一个显示温度,一个显示湿度,一个显示压力。我的目标是,每次点击其中一个按钮都会删除当前的显示(例如,它正在显示温度,而我想要显示湿度)。我用函数尝试了一下:
def clear_display():
inside_temp_display.config(text="")
outside_temp_display.config(text="")
diff_temp_display.config(text="")
inside_humid_display.config(text="")
outside_humid_display.config(text="")
diff_humid_display.config(text="")
inside_press_display.config(text="")
outside_press_display.config(text="")
diff_press_display.config(text="")
but everytime I run the program I get the following error message:
但每次运行该程序时,我都会收到以下错误消息:
File "/home/pi/Documents/Meteo/meteo_tkinter.py", line 71, in clear_display
inside_temp_display.config(text="")
NameError: name 'inside_temp_display' is not defined
What am I doing wrong in the code?
P.S. I am using Tkinter and SenseHat Emulator. Here is the full code:
我在代码中做错了什么?附注:我正在使用Tkinter和SenseHat Emulator。以下是完整的代码:
from sense_emu import SenseHat
from tkinter import *
import threading
import requests
import datetime as datetime
sense = SenseHat()
# ---- INSIDE TEMPERATURE, HUMIDTY AND PRESSURE ---- #
inside_temp = round(sense.get_temperature(), 2)
inside_humid = round(sense.get_humidity(), 2)
inside_press = round(sense.get_pressure(), 2)
# ---- OUTSIDE TEMPERATURE, HUMIDTY AND PRESSURE ---- #
url = 'https://api.open-meteo.com/v1/forecast?latitude=45.8144&longitude=15.978&hourly=temperature_2m,relativehumidity_2m,surface_pressure&timezone=Europe%2FBerlin&forecast_days=1'
response = requests.get(url)
data = response.json()
# ---- CALCULATE DIFFERENCES ---- #
outside_temp_hourly = data['hourly']['temperature_2m']
outside_humid_hourly = data['hourly']['relativehumidity_2m']
outside_press_hourly = data['hourly']['surface_pressure']
temp_diff = [round(temp - inside_temp, 2) for temp in outside_temp_hourly]
humid_diff = [round(hum - inside_humid, 2) for hum in outside_humid_hourly]
press_diff = [round(press - inside_press, 2) for press in outside_press_hourly]
# ---- GET CURRENT HOUR ---- #
now = datetime.datetime.now()
current_hour = now.hour
# ---- FUNCTIONS FOR THE BUTTONS ---- #
def show_current_temp():
clear_display()
inside_temp_display = Label(text = f"Current inside temperature: {inside_temp}°C.", pady=5)
inside_temp_display.grid(row=2, column=1)
outside_temp_display = Label(text = f"Current outside temperature: {outside_temp_hourly[current_hour]}°C.", pady=5)
outside_temp_display.grid(row=3, column=1)
diff_temp_display = Label(text = f"Difference in temperature is: {abs(temp_diff[current_hour])}°C.", pady=5)
inside_temp_display.grid(row=4, column=1)
def show_current_humid():
clear_display()
inside_humid_display = Label(text = f"Current inside humidity: {inside_humid}%.", pady=5)
inside_humid_display.grid(row=2, column=1)
outside_humid_display = Label(text = f"Current outside humidity: {outside_humid_hourly[current_hour]}%.", pady=5)
outside_humid_display.grid(row=3, column=1)
diff_humid_display = Label(text = f"Difference in humidty is: {abs(humid_diff[current_hour])}%.", pady=5)
inside_humid_display.grid(row=4, column=1)
def show_current_press():
clear_display()
inside_press_display = Label(text = f"Current inside pressure: {inside_press}hPa.", pady=5)
inside_press_display.grid(row=2, column=1)
outside_press_display = Label(text = f"Current outside pressure: {outside_press_hourly[current_hour]}hPa.", pady=5)
outside_press_display.grid(row=3, column=1)
diff_press_display = Label(text = f"Difference in pressure is: {abs(press_diff[current_hour])}hPa.", pady=5)
inside_press_display.grid(row=4, column=1)
def clear_display():
inside_temp_display.config(text="")
outside_temp_display.config(text="")
diff_temp_display.config(text="")
inside_humid_display.config(text="")
outside_humid_display.config(text="")
diff_humid_display.config(text="")
inside_press_display.config(text="")
outside_press_display.config(text="")
diff_press_display.config(text="")
# ---- MAIN GUI PROGRAM ---- #
def gui():
root = Tk()
root.title("Weather")
root.geometry("700x600")
title = Label(root, text="Weather info", font=("Arial", 20), pady=20)
title.grid(row=0, column=1)
temp_button = Button(root, text="Display temperature", command = show_current_temp)
temp_button.grid(row=1, column=0, padx=40)
humid_button = Button(root, text="Display humidity", command = show_current_humid)
humid_button.grid(row=1, column=1, padx=45, pady=20)
press_button = Button(root, text="Display pressure", command = show_current_press)
press_button.grid(row=1, column=2, padx=40)
root.mainloop()
I tried a lot of combinations but non of them worked.
我尝试了很多组合,但都没有奏效。
更多回答
inside_temp_display
is a local variable inside some other function. It does not exist outside of that function.
INSIDE_TEMP_DISPLAY是其他函数中的局部变量。它不存在于该函数之外。
It is a label inside show_curren_temp function.
它是show_Curren_Temp函数中的标签。
@Frane it's a local variable that only exists inside show_curren_temp
, but you're trying to use it inside clear_display
.
@Frane它是一个局部变量,只存在于show_Curren_temp中,但您正试图在Clear_Display中使用它。
how do I make it global, sorry I am new to programming and Python?
我如何让它全球化,对不起,我是编程和Python的新手?
Move this inside_temp_display = Label(text = f"Current inside temperature: {inside_temp}°C.", pady=5) inside_temp_display.grid(row=2, column=1) outside of function.
将INSIDE_TEMP_DISPLAY=LABEL(Text=f“Current Inside Temp:{Inside_Temp}°C”,PADY=5)Inside_Temp_Display.grid(ROW=2,COLUMN=1)移到函数外部。
As John Gordon mentioned, inside_temp_display
is defined and thus only recognized inside the scope of show_current_temp()
. You ca do
正如John Gordon提到的,INSIDE_TEMP_DISPLAY是定义的,因此只能在show_Current_Temp()的作用域内识别。你做得到
def f1():
global x
x = 1
print(x, "in f1")
def f2():
global x
print(x)
x = 10
print(x)
f1()
f2()
1 in f1
1
10
to let x be shared among different scopes, which is a more risky way when you project is complex.
让x在不同的作用域之间共享,当您的项目很复杂时,这是一种更危险的方式。
Another way is passing your inside_temp_display
to show_current_temp()
as a parameter.
另一种方法是将Inside_Temp_Display作为参数传递给show_Current_Temp()。
def clear_display(inside_temp_display):
inside_temp_display.config(text="")
Again another way is to do some OOP to let data member be shared, for example:
同样,另一种方法是执行一些面向对象的操作,以共享数据成员,例如:
class GreatPlot:
def __init__():
self.inside_temp_display = None
def show_current_temp():
self.inside_temp_display = Label(text = f"Current inside temperature: {inside_temp}°C.", pady=5)
def clear_display():
self.inside_temp_display.config(text="")
my_great_plot = GreatPlot()
my_great_plot.show_current_temp()
my_great_plot.clear_display()
For global
and nonlocal
, see ref: https://stackoverflow.com/a/1261961/8329196
有关全局和非本地的信息,请参阅REF:https://stackoverflow.com/a/1261961/8329196
更多回答
Are you sure the OOP example is correct?
您确定OOP示例是正确的吗?
I will try on of these options and let you know if it works. Thank you.
我会试一试这些选项,然后让你知道它是否有效。谢谢。
@DelriusEuphoria I edited a little bit to make the code formal. Thanks. Feel free to suggest to improve that!
@DelriusEveloria我稍微编辑了一下,使代码变得正式。谢谢。请随时建议改进这一点!
我是一名优秀的程序员,十分优秀!