python中stringvar不能修改_如何处理“StringVar”类型的Python

我正在创建一个代码,可以将温度单位转换为其他温度单位,例如摄氏度到华氏度。当我运行这段代码时,我得到一个错误,说明类型错误:unhashable type StringVar。我不知道我做错了什么,也不知道如何解决这个问题,任何帮助都将不胜感激。在from tkinter import *

import tkinter as tk

from tkinter import ttk

root = tk.Tk()

#======================================================================

notebook = ttk.Notebook(root)

frame3 = ttk.Frame(notebook)

notebook.add(frame3, text='Temperature')

notebook.pack(expand=1, fill="both")

#======================================================================

def Temperature_converter(*args):

v = float(temp_entry.get())

temp_dict = dict(Fahrenheit= (1/1.8, -32/1.8), Celsius= (1, 0), Kelvin= (1, -273.15))

x, y = temp_dict[temp_var1]

cels = temp_entry * x + y #turns input to celsius by mapping x and y to ratio and difference

x, y = temp_dict[temp_var2]

answer = (cels - y) / x # turns input in celsius to output

temp_label['text']=answer

#======================= ===============================================

temp_entry = Entry(frame3)

temp_entry.grid(row=0, column=0)

temp_label = Label(frame3, relief='groove', width=20, text='')

temp_label.grid(row=0, column=3)

options3 = ['Unit', 'Celsius', 'Fahrenheit', 'Kelvin']

temp_var1 = tk.StringVar(frame3)

temp_var1.set(options3[0])

temp_dropdown1 = tk.OptionMenu(frame3, temp_var1, options3[1], options3[2], options3[3])

temp_dropdown1.grid(row=1, column=0)

temp_var2 = tk.StringVar(frame3)

temp_var2.set(options3[0])

temp_dropdown2 = tk.OptionMenu(frame3, temp_var2, options3[1], options3[2], options3[3])

temp_dropdown2.grid(row=1, column=3)

temp_equal_button = Button(frame3, text='=', command=Temperature_converter)

temp_equal_button.grid(row=1, column=5)

#======================================================================

root.mainloop

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值