2012-04-09 113 views
2

下面是一些示例代碼,一切工作到菜單和gui的函數,但函數不起作用時,你傳遞值,當我想要做的是計算問題,因爲它的目的。Python Tkinter菜單欄

我希望功能是一個單獨的程序,到主窗口,因爲它是。 原因是我想將更多這些數學函數添加到菜單中。

我該如何獲得此功能?我新,去容易請.... 預先感謝您...

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() 

回答

2
from Tkinter import * 
import ttk 
import tkMessageBox 

def DVT(): 

    vel = StringVar() 
    tme = StringVar() 
    dist = StringVar() 

    def dvt(): 
     """This Function will determine distance in feet from a constant velocity and time""" 
     try: 
      val1 = float(vel_entry.get()) 
      val2 = float(tme_entry.get()) 
      dist.set(val1 * val2) 
      tkMessageBox.showinfo(None,"The Distance is: " + dist.get()) 
     except ValueError as v: 
      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_entry = ttk.Entry(mainframe, width=7) 
    vel_entry.grid(column=2, row=1, sticky=(E, W)) 
    tme_entry = ttk.Entry(mainframe, width=7) 
    tme_entry.grid(column=2, row=2, sticky=(E, W)) 


    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) 

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() 
+5

您可以編輯這個答案來解釋代碼,爲什麼它適用? – 2012-04-09 05:39:18

+0

非常感謝你! – tjrob 2012-04-09 06:36:10

+1

@tjrob如果這個答案有幫助,你可以考慮「接受它」(勾出十字把它變成綠色)。請參閱http://stackoverflow.com/faq#reputation – FabienAndre 2012-04-14 12:08:34