Python ';选项菜单';对象没有属性';获取';
Python 3.9 视窗10 VSCode 嘿,伙计们,我正在tkinter为一个学校项目制作一个天气转换器,谷歌和我的老师都无法解决这个问题 每当我在两个下拉菜单中输入一个值并点击convert时,我都会得到错误: “OptionMenu”没有属性“get” 这是我的密码; 关键区域是检查输入定义和第68-97行的菜单Python ';选项菜单';对象没有属性';获取';,python,tkinter,optionmenu,Python,Tkinter,Optionmenu,Python 3.9 视窗10 VSCode 嘿,伙计们,我正在tkinter为一个学校项目制作一个天气转换器,谷歌和我的老师都无法解决这个问题 每当我在两个下拉菜单中输入一个值并点击convert时,我都会得到错误: “OptionMenu”没有属性“get” 这是我的密码; 关键区域是检查输入定义和第68-97行的菜单 from tkinter import * from PIL import ImageTk, Image # Dropdown menu options Temp_Opti
from tkinter import *
from PIL import ImageTk, Image
# Dropdown menu options
Temp_Options = ["Celsius", "Fahrenheit", "Kelvin"]
root = Tk()
root.title("The World\'s Best Temperature Converter")
root.geometry("575x250")
root.eval('tk::PlaceWindow . center')
# Checks what units were input
def Check_Input():
print (Input_Value.get)
if Input_Value.get == "Celsius":
print("Input Recieved = Celsius")
Celsius()
if Input_Value.get == "Fahrenheit":
print("Input Recieved = Fahrenheit")
Fahrenheit()
if Input_Value.get == "Kelvin":
Kelvin()
print("Input Recieved = Kelvin")
else:
print("Check Input Failed")
# Convert FROM Celsius
def Celsius():
Temp = User_Input.get()
if Output_Value.get == "Celsius":
Answer = ((Temp * 9/5) + 32)
if Output_Value.get == "Fahrenheit":
Answer = (Temp - 32 * (9/5))
if Output_Value.get == "Kelvin":
Answer = (Temp - 273.15)
Final_Answer = Label(root, text=Answer)
Final_Answer.grid(row=3, column=1)
# Convert FROM Fahrenheit
def Fahrenheit():
Temp = User_Input.get()
if Output_Value.get == "Celsius":
Answer = ((Temp * 9/5) + 32)
if Output_Value.get == "Fahrenheit":
Answer = (Temp - 32 * (9/5))
if Output_Value.get == "Kelvin":
Answer = (Temp - 273.15)
Final_Answer = Label(root, text=Answer)
Final_Answer.grid(row=3, column=1)
# Convert FROM Kelvin
def Kelvin():
Temp = User_Input.get()
if Output_Value.get == "Celsius":
Answer = ((Temp * 9/5) + 32)
if Output_Value.get == "Fahrenheit":
Answer = (Temp - 32 * (9/5))
if Output_Value.get == "Kelvin":
Answer = (Temp - 273.15)
Final_Answer = Label(root, text=Answer)
Final_Answer.grid(row=3, column=1)
# Choose Default for Input options
Input_Value = StringVar(root)
Input_Value.set("Select Input")
# Input Options Menu
Input_Value = OptionMenu(
root, Input_Value, *Temp_Options)
Input_Value.config(width=9)
Input_Value.grid(row=1, column=0)
# Chose Default for Output Options
Output_Value = StringVar(root)
Output_Value.set("Select Output")
# Output options menu
Output_Value = OptionMenu(
root, Output_Value, *Temp_Options)
Output_Value.config(width=9)
Output_Value.grid(row=1, column=2)
# Text Input Box
User_Input = Entry(root, width=50, borderwidth=10)
User_Input.grid(row=1, column=1)
# Button
Convert = Button(root, text="Convert", command=Check_Input)
Convert.grid(row=1, column=3)
root.mainloop()
提前感谢,如果这个问题很愚蠢,请道歉。正如acw1668在评论中所说的那样 我对StringVar和OptionMenu使用了相同的名称 在整个项目中更改它可以修复所有问题
另外,我现在意识到,我的代码的数学部分都搞糟了,但这是我可以自己解决的。您对
StringVar
和OptionMenu
使用了相同的名称Input\u Value
。输出值也一样。你是个救命的家伙,这完全解决了它!