Python Tkinter Entry为以后

时间:2017-08-15 23:44:15

标签: python tkinter

我对Tkinter很新,但是我怎么能得到我输入的所有Entry命令以便以后保存为变量?这是条目存在的代码。此程序旨在跟踪进度,并使用整数而不是浮点。感谢

然后使用存储的变量供以后用于其他组件。

  name = Tk()
  name.wm_title("Name")

  Label(name, text="                       First").grid(row=0, column=0)
  Label(name, text="                        Last").grid(row=0, column=2)
  Label(name, text="             Starting height").grid(row=1, column=0)
  Label(name, text="                  Now height").grid(row=1, column=2)
  Label(name, text="                Starting Age").grid(row=2, column=0)
  Label(name, text="                     Now Age").grid(row=2, column=2)
  Label(name, text="        Starting Bench Press").grid(row=3, column=0)
  Label(name, text="   Personal best Bench press").grid(row=3, column=2)
  Label(name, text="     Starting Weighted Squat").grid(row=4, column=0)
  Label(name, text="Personal best weighted squat").grid(row=4, column=2)
  Label(name, text="           Starting deadlift").grid(row=5, column=0)
  Label(name, text="      Personal best deadlift").grid(row=5, column=2)
  Label(name, text="     Starting shoulder press").grid(row=6, column=0)
  Label(name, text="Personal best shoulder press").grid(row=6, column=2)
  Label(name, text="          Starting leg press").grid(row=7, column=0)
  Label(name, text="     Personal best leg press").grid(row=7, column=2)
  Label(name, text="       Starting lat pulldown").grid(row=8, column=0)
  Label(name, text="  Personal best lat pulldown").grid(row=8, column=2)


  first                    = Entry(name)
  last                     = Entry(name)
  starting_height          = Entry(name)
  Now_height               = Entry(name)
  Start_age                = Entry(name)
  Now_age                  = Entry(name)
  Start_Bench_Press        = Entry(name)
  Heaviest_Bench_Press     = Entry(name)
  Start_Weight_Squat       = Entry(name)
  Heaviest_Weight_Squat    = Entry(name)
  Start_deadlift           = Entry(name)
  Heaviest_deadlift        = Entry(name)
  Start_Shoulder_Press     = Entry(name)
  Heaviest_Shoulder_Press  = Entry(name)
  Start_Leg_Press          = Entry(name)
  Heaviest_Leg_Press       = Entry(name)
  Start_Lat_Pulldown       = Entry(name)
  Heaviest_Lat_Pulldown    = Entry(name)

  first.grid                   (row=0, column=1)
  last.grid                    (row=0, column=3)
  Starting_height.grid         (row=1, column=1)
  Now_height.grid              (row=1, column=3)
  Start_age.grid               (row=2, column=1)
  Now_age.grid                 (row=2, column=3)
  Start_Bench_Press.grid       (row=3, column=1)
  Heaviest_Bench_Press.grid    (row=3, column=3)
  Start_Weight_Squat.grid      (row=4, column=1)
  Heaviest_Weight_Squat.grid   (row=4, column=3)
  Start_deadlift.grid          (row=5, column=1)
  Heaviest_deadlift.grid       (row=5, column=3)
  Start_Shoulder_Press.grid    (row=6, column=1)
  Heaviest_Shoulder_Press.grid (row=6, column=3)
  Start_Leg_Press.grid         (row=7, column=1)
  Heaviest_Leg_Press.grid      (row=7, column=3)
  Start_Lat_Pulldown.grid      (row=8, column=1)
  Heaviest_Lat_Pulldown.grid   (row=8, column=3)


  Button(name, text='Quit', command=name.destroy).grid(row=10, column=0, sticky=W, pady=4)

  name.mainloop()

1 个答案:

答案 0 :(得分:0)

以下是使用IntVar保存Entry小部件值的示例。

from tkinter import *
from tkinter import messagebox

name = Tk()
name.wm_title("Name")

Label(name, text="sFirst").grid(row=0, column=0)

firstsv = IntVar()
first = Entry(name, textvariable=firstsv)

first.grid(row=0, column=1)

b1 = Button(name, text='Show', command=lambda: messagebox.showinfo("hi", firstsv.get()))
b1.grid(row=1, column=0, sticky=W, pady=4)

name.mainloop()

# use after loop
print('firstsv = %d' % firstsv.get())

这是python 3代码。如果你正在使用python 2(许多初学者似乎还在做),那么它就是一个转换代码的练习。