我想在组合框中选择“键”后获取“值”。例如,在第一个组合框中,我选择“ b”,在第二个组合框中,我选择“ e”,我想将这两个选项的总和(“ 7”)作为结果。我不知道“ num1”和“ num2”应该放在什么位置,或者我需要以其他方式进行操作?
'''python
import tkinter as tk
from tkinter import ttk
def main():
zestawienie={
"a": "1",
"b": "2",
"c": "3"
}
zestawienie2={
"d": "4",
"e": "5",
"f": "6"
}
root = tk.Tk()
root.title('Energy')
root.geometry('500x200')
def add():
blank.delete(0, tk.END)
Ans = float(num1) + float(num2)
blank.insert(0, Ans)
tk.Label(root, text="Rodzaj źródła ciepła",bd=3).grid(column=0,row=0)
number1 = tk.StringVar()
tk.Label(root, text="System przyhotowania ciepłej wody").grid(column=0,row=2)
number2= tk.StringVar()
combo1 = ttk.Combobox(root,values=list(zestawienie.keys()),justify="center",textvariable=number1)
combo1.bind('<<ComboboxSelected>>', lambda event: label_selected.config(text=zestawienie[number1.get()]))
combo1.grid(column=0,row=1)
combo1.current(0)
combo2 = ttk.Combobox(root,values=list(zestawienie1.keys()),justify="center",textvariable=number2)
combo2.bind('<<ComboboxSelected>>', lambda event: label_selected1.config(text=zestawienie1[number2.get()]))
combo2.grid(column=0,row=3)
combo2.current(0)
label_selected = tk.Label(root, text="Not Selected")
label_selected.grid(row=0, column=3)
label_selected1 = tk.Label(root, text="Not Selected")
label_selected1.grid(row=0, column=4)
#num1=
#num2=
blank = tk.Entry(root)
ttk.Label(root, text = "The Answer is:").grid(row=4)
blank.grid(row=4, column=1)
tk.Button(root, text='Add', command=add).grid(row=4, column=3)
root.mainloop()
if __name__ == '__main__':
main()
'''
答案 0 :(得分:0)
我建议您首先更改dict
中的值。使用int
或float
代替str
,因为您将要对这些值进行计算。
zestawienie={"a": 1, "b": 2, "c": 3}
zestawienie2={"d": 4, "e": 5, "f": 6}
接下来,您的代码中出现了一些错误,例如使用了错误的变量名。我必须修复它才能使GUI工作。修复这些问题后,可以通过以下方法解决您提出的问题:
def add():
blank.delete(0, tk.END)
Ans = zestawienie.get(number1.get()) + zestawienie2.get(number2.get())
blank.insert(0, Ans)
完整代码:
import tkinter as tk
from tkinter import ttk
def main():
zestawienie = {"a": 1, "b": 2, "c": 3}
zestawienie2 = {"d": 4, "e": 5, "f": 6}
root = tk.Tk()
root.title('Energy')
root.geometry('500x200')
def add():
blank.delete(0, tk.END)
Ans = zestawienie.get(number1.get()) + zestawienie2.get(number2.get())
blank.insert(0, Ans)
tk.Label(root, text="Rodzaj źródła ciepła",bd=3).grid(column=0,row=0)
number1 = tk.StringVar()
tk.Label(root, text="System przyhotowania ciepłej wody").grid(column=0,row=2)
number2= tk.StringVar()
combo1 = ttk.Combobox(root,values=list(zestawienie.keys()),justify="center",textvariable=number1)
combo1.bind('<<ComboboxSelected>>', lambda event: label_selected.config(text=zestawienie[number1.get()]))
combo1.grid(column=0,row=1)
combo1.current(0)
combo2 = ttk.Combobox(root,values=list(zestawienie2.keys()),justify="center",textvariable=number2)
combo2.bind('<<ComboboxSelected>>', lambda event: label_selected1.config(text=zestawienie2[number2.get()]))
combo2.grid(column=0,row=3)
combo2.current(0)
label_selected = tk.Label(root, text="Not Selected")
label_selected.grid(row=0, column=3)
label_selected1 = tk.Label(root, text="Not Selected")
label_selected1.grid(row=0, column=4)
blank = tk.Entry(root)
ttk.Label(root, text = "The Answer is:").grid(row=4)
blank.grid(row=4, column=1)
tk.Button(root, text='Add', command=add).grid(row=4, column=3)
root.mainloop()
if __name__ == '__main__':
main()