值未存储在 Tkinter 变量中

Values not stored in Tkinter Variables

在我的代码中,我尝试通过文本字段获取用户输入,将它们存储在变量中,最后以表格形式打印。 我面临的问题是显示了我通过文本字段输入的值中的 none;当我尝试打印变量时,它们是空的。 这是我的部分代码:

# SPASC

from tkinter import *
import tkinter as tk
import tkinter.ttk as tktrv

root = tk.Tk()
root.title("SPASC")
root.geometry("410x400")
lb1 = Label(root, text="SPASC \n Welcomes You !!!", fg="red", bg="sky blue"
        , font=('Arial Black', 20), width=22, anchor=CENTER)
lb2 = Label(root, text="What would you like to compare?",
        font=('Arial', 18), anchor=CENTER)
space1 = Label(root, text="\n\n")
lb1.grid(row=0)
lb2.grid(row=5)
space1.grid(row=1)
hpw, mil = StringVar(), StringVar()

def bt_cars():
   w1 = Toplevel()
   w1.title("Choose Features")
   w1.geometry("430x200")

   lb3 = Label(w1, text="Choose features for comparison", bg="yellow"
            , font=('Arial Black', 18), width=25)
   lb4 = Label(w1, text=" ", anchor=CENTER)
   fr1 = LabelFrame(w1, width=20, padx=100)
   hpw_cb = Checkbutton(fr1, text="Horsepower", variable=hpw, anchor='w', onvalue="Horsepower", offvalue="")
   hpw_cb.grid()
   hpw_cb.deselect()
   mil_cb = Checkbutton(fr1, text="Mileage", variable=mil, anchor='w', onvalue="Mileage", offvalue="")
   mil_cb.grid()
   mil_cb.deselect()
   var_stor = [hpw, mil]
   print(hpw)
   print(mil)
   var_fill = []
   for itr1 in var_stor:
       if itr1 != "":
           var_fill.append(itr1)
   print(var_fill)
   def car_1():
       name1 = StringVar()
       c1 = Toplevel()
       c1.title("Car 1")
       c1.geometry("430x200")
       car1_lb1 = Label(c1, text="Car Name:")
       name1_ifl = Entry(c1)
       name1 = name1_ifl.get()

       elm_var_fill = len(var_fill)
       ct1 = 0
       car1_val = []
       for itr2 in var_fill:
           if ct1 == elm_var_fill:
              break
           lb5 = Label(c1, text=itr2.get())
           #Creating text field
           ftr1_ifl = Entry(c1)
           car1_ftr = ftr1_ifl.get()
           car1_val.append(car1_ftr)
           car1_ftr = None
           lb5.grid(row=ct1 + 2, column=1)
           ftr1_ifl.grid(row=ct1 + 2, column=2)
           ct1 += 1
       print(car1_val)

       def display():
           dp = Toplevel()
           dp.title("Results")
           dp.geometry("500x200")
           car1_pt = 0
           car2_pt = 0
           car_tree = tktrv.Treeview(dp)
           car_tree["columns"] = ("car1col")
           car_tree.column("#0", width=120, minwidth=30)
           car_tree.column("car1col", width=120, minwidth=30)
           car_tree.heading("#0", text="Features" )
           car_tree.heading("car1col", text=str(name1))
           car_tree.pack()
           c1.withdraw()
           print(var_fill)
      done1_bt = Button(c1, text="Continue", command=display)
      name1_ifl.grid(row=0, column=2)
      car1_lb1.grid(row=0, column=1)
      done1_bt.grid(row=5,column=1)
      w1.withdraw()

   done_bt = Button(w1, text="Done", command=car_1)
   done_bt.grid(row=3, column=1)
   lb3.grid(row=0, column=1)
   lb4.grid(row=1, column=1)
   fr1.grid(row=2, column=1)
   root.withdraw()

bt1 = Button(root, text="CARS", width=5, font=('Calibri', 15), command=bt_cars)
bt1.grid(row=7)
space2 = Label(root, text="\n\n")
space2.grid(row=6)
root.mainloop()

我遇到名为 hpw、mil、name1 的变量的问题。 欢迎任何帮助。

注意:- 请原谅代码量;我希望其他人复制错误并自己查看

问题是您使用 get() 错误。您不能在 Entry() 之后立即使用 get(),因为一旦创建条目,它就会在用户输入内容之前获取输入。

使用此代码:

def get_input(text):
  print(text)

e = Entry(root)
e.pack()
b = Button(root, text="Print input", command=lambda: get_input(e.get()))
b.pack()

现在 get() 方法在您单击按钮之前不会执行。

对于变量 hpwmil,这些变量是空字符串,这就是您没有从这些复选框中获得任何值的原因。要从复选框中获取值,请替换这些代码行:

var_stor = [hpw, mil]

var_stor = [hpw_cb.cget('onvalue'), mil_cb.cget('onvalue')]

既然你想要 onvalue 那么你必须使用 cget() 方法来访问这些值。

另外,替换

lb5 = Label(c1, text=itr2.get())

lb5 = Label(c1, text=itr2)

因为现在列表中有所需的值(不是对象),所以只需要访问这些值。

变量name1可以使用@BokiX的方法