不好意思再次在Python中遇到我的新手问题。我正在尝试使用组合框在Python中进行简单的计算(乘法),我不知道如何。下面你会发现我迄今为止所做的一切都没有成功。希望你能帮我!
非常感谢你提前
这是我的代码:
import Tkinter
root = Tk()
root.title("Model A")
root.minsize(400, 220)
root.maxsize(410, 240)
# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill='both', expand='yes')
notebook.pressed_index = None
# child frames
frameOne = Tkinter.Frame(notebook, bg='white',width=560, height=100)
frameOne.pack(fill='both', expand=True)
# pages
notebook.add(frameOne, text='Simple calculation')
#Close Application Button
def quit(root):
root.destroy()
tk.Button(root, text="Close Application", command=lambda root=root:quit(root)).pack()
## Calculation
def defocus(event):
event.widget.master.focus_set()
def multiply(*args):
try:
product.config(round(float(Num_One.get())*float(Num_Two.get())))
except ValueError:
pass
## variables
Num_One = StringVar()
Num_Two = StringVar()
product = DoubleVar()
#Number One
ttk.Label(frameOne, text="Select First Number:").grid(column =3, row = 0)
NumOne_Select = Combobox(frameOne, values=("1", "2", "3","4", "5"),textvariable=Num_One)
NumOne_Select.grid(column=4, row=0, columnspan="5", sticky="nswe")
NumOne_Select.bind('', multiply)
NumOne_Select.bind('', multiply)
#Number two
ttk.Label(frameOne, text="Select Second Number:").grid(column =3, row = 6 )
NumTwo_Select = Combobox(frameOne, values=("1", "2", "3","4", "5"),textvariable=Num_Two)
NumTwo_Select.grid(column=4, row=6, columnspan="5", sticky="nswe")
NumTwo_Select.bind('', multiply)
NumTwo_Select.bind('', multiply)
# display results
ttk.Label(frameOne, text = "Product:").grid(column = 3, row = 8)
ttk.Label(frameOne, textvariable=product).grid(column = 4, row = 8)
root.mainloop()