2

以下はいくつかのサンプルコードです。メニューと関数の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()
4

1 に答える 1

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()
于 2012-04-09T05:11:13.207 に答える