繁体   English   中英

如何从 Tkinter 的输入框中获取值?

[英]How to get the Values from the Entry box in Tkinter?

def in_Vals():
    in_win = Tk()
    in_win.title("Check In Details")
    in_win.geometry("700x700")
    in_win.resizable(0,0)

    # title

    title = Label(in_win,text="Check In Details",font=("Harlow Solid Italic",30,"italic"),fg="black",bg="#fbb08c")
    title.pack(anchor="center",pady=5)

    #creating label's
    _Id_ = Label(in_win,text="Id :",font=("Times New Roman",15,"italic"),fg="black",bg="#fbb08c")
    _Name_ = Label(in_win,text="Name :",font=("Times New Roman",15,"italic"),fg="black",bg="#fbb08c")
    _Date_ = Label(in_win,text="Date :",font=("Times New Roman",15,"italic"),fg="black",bg="#fbb08c")
    _Time_ = Label(in_win,text="Time :",font=("Times New Roman",15,"italic"),fg="black",bg="#fbb08c")
    _Number_ = Label(in_win,text="Number :",font=("Times New Roman",15,"italic"),fg="black",bg="#fbb08c")
    
    
    _Id_.pack(anchor='w',padx=10,pady=20)
    _Name_.pack(anchor='w',padx=10,pady=20)
    _Date_.pack(anchor='w',padx=10,pady=20)
    _Time_.pack(anchor='w',padx=10,pady=20)
    _Number_.pack(anchor='w',padx=10,pady=20)

    # creating submit function

    def submit():
        print(f"{in_val_1}\n{in_val_2}\n{in_val_3}\n{in_val_4}\n{in_val_5}")


    # creating entries

    Id = Entry(in_win,width=25,font=("Courier",15,'bold'))
    Name = Entry(in_win,width=25,font=("Courier",15,'bold'))
    Date = Entry(in_win,width=25,font=("Courier",15,'bold'))
    Time = Entry(in_win,width=25,font=("Courier",15,'bold'))
    Number = Entry(in_win,width=25,font=("Courier",15,'bold'))

    Id.place(x=100,y=87)
    Name.place(x=100,y=157)
    Date.place(x=100,y=227)
    Time.place(x=100,y=293)
    Number.place(x=100,y=360)

    #getting values

    in_val_1 = Id.get()
    in_val_2 = Name.get()
    in_val_3 = Date.get()
    in_val_4 = Time.get()
    in_val_5 = Number.get()


    # creating submit button

    submit = Button(in_win,text="Submit",font=("Wild Latin",15,"bold"),command=submit)
    submit.place(x = 250,y=450)
    
    in_win.config(bg="#fbb08c")
    in_win.mainloop()

这里 function in_vals()是一个编码,用于从 ID、名称、日期、时间、数字条目中获取数据,并将条目的值分配给变量in_val_1 到 in_val_5 ,以从我使用的条目框中获取值.get()方法。 但是当我尝试打印分配给.get()方法的变量时,它会打印一些空白。

和我一样的问题的解决方案是

在按钮 function 之外定义值不会得到任何结果。

在这里我定义了按钮 function

在按钮 function 内定义它后,它给了我所需的 output

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM