簡體   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