Python Tkinter菜单栏

时间:2012-04-09 03:19:54

标签: python tkinter

下面是一些示例代码,一切都可以使用菜单,以及函数的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()

1 个答案:

答案 0 :(得分: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()