很抱歉再次因我在 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('<KeyPress>', multiply)
NumOne_Select.bind('<KeyRelease>', 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('<KeyPress>', multiply)
NumTwo_Select.bind('<KeyRelease>', 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()