下面是一些示例代码,一切都适用于菜单和函数的 gui,但是当你传递值时函数不起作用,当我想要它做的是计算问题时,因为它是设计的至。
我希望该功能是一个单独的程序,对主窗口来说是这样。原因是我想将更多这些数学函数添加到菜单中。
我怎样才能让这个功能工作?我是新手,请放轻松....提前谢谢您...
from Tkinter import *
import ttk
def DVT():
def dvt(*args):
"""This Function will determine distance in feet from a constant velocity and time"""
try:
val1 = float(vel.get())
val2 = float(tme.get())
dist.set(val1 * val2)
except ValueError:
pass
root = Tk()
root.title('Distance from Velocity and Time')
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight=1)
vel = StringVar()
tme = StringVar()
dist = StringVar()
vel_entry = ttk.Entry(mainframe, width=7, textvariable=vel)
vel_entry.grid(column=2, row=1, sticky=(E, W))
tme_entry = ttk.Entry(mainframe, width=7, textvariable=tme)
tme_entry.grid(column=2, row=2, sticky=(E, W))
ttk.Label(mainframe, textvariable=dist).grid(column=2, row=3, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=dvt).grid(column=3, row=2, sticky=W)
ttk.Label(mainframe, text="Velocity").grid(column=1, row=1, sticky=(W, E))
ttk.Label(mainframe, text="Time").grid(column=1, row=2, sticky=W)
ttk.Label(mainframe, text="The Distance is:").grid(column=1, row=3, sticky=E)
for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
vel_entry.focus()
tme_entry.focus()
root.bind('<Return>', dvt)
root.mainloop()
root = Tk()
menubar = Menu(root)
vel = StringVar()
tme = StringVar()
dist = StringVar()
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Distance from Velocity and Time", command=DVT)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_separator()
root.config(menu=menubar)
root.mainloop()