[英]How do I relate the string I get from the combobox to a specific float I want in Python?
Here's the situation: 情况如下:
I need to get a value (float) that is different from the selected option in the combobox (a string) and I don't know how to do it. 我需要获取与组合框(字符串)中所选选项不同的值(浮点数),我不知道该怎么做。
from tkinter import *
from tkinter import ttk
combo = ttk.Combobox(top, state="readonly")
combo["values"] = ("DAP", "MAP", "SPS", "SPT")
combo.current(0)
combo.grid(row=row, column=1)
DAP=46 DAP = 46
MAP=52 MAP = 52
SPS=21 SPS = 21
SPT=46 SPT = 46
combo.get()
will fetch me "DAP" (if DAP is selected in the combobox obviously) and I want to get 46 in this case. combo.get()
将获取“ DAP”(如果显然在组合框中选择了DAP),在这种情况下,我想获取46。
So when an option is chosen from the combobox I need to get a float (eg when someone selects DAP in the combobox I need to get or get returned 46 as a float). 因此,当从组合框中选择一个选项时,我需要获取一个浮动(例如,当某人在组合框中选择DAP时,我需要获取或返回46作为浮动)。 With
combo.get()
I'll get 'DAP' (If DAP is selected for example) and I want to get 46 instead (as a float).I need to relate each gotten string with the corresponding float. 使用
combo.get()
我会得到'DAP'(例如,如果选择了DAP),我想得到46(作为浮点数)。我需要将每个获得的字符串与相应的浮点数相关联。
I use it on this formula: 我在此公式上使用它:
dosis = 50 / float(combo_dict.get((combo.get())) / 100)
Use a dict
( [Python]: Mapping Types — dict ): 使用
dict
( [Python]:映射类型— dict ):
combo_dict = {
"DAP": 46,
"MAP": 52,
"SPS": 21,
"SPT": 46,
}
and its usage (as current values are int
s, convert them to float
s): 及其用法(由于当前值为
int
,请将其转换为float
):
float(combo_dict[combo.get()])
or better (in case the string returned by the combo is not present in combo_dict
: [Python]: get ( key[, default] ) ): 或更好(如果
combo_dict
不存在组合返回的字符串: [Python]: get ( key [,default] ) ):
float(combo_dict.get(combo.get(), -1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.