我正在为我的应用程序构建GUI,但得到错误.为什么?



我正在尝试使用tkinter为我的应用程序制作GUI,但它不起作用。为什么?程序需要从用户获取2个输入并将它们保存在变量中,我还标记了下面代码中出现错误的地方

import tkinter as tk
# making the window
root = tk.Tk()
root.title("AutoWhatsUp")
root.geometry('500x500')
# getting phone number from user
enter_number = tk.Label(root, text = "enter below the phone number you want to message")
enter_number.pack()
filed = tk.Entry(root)
filed.pack()
def get_number():
phone_num = filed.get()
done_procces_phone = tk.Label(root, text = 'Phone number procced!').pack() # getting error here
confirm_number = tk.Button(root, text = 'procces number', command = get_number).pack() #getting error here
# getting the message
enter_mess = tk.Label(root, text = 'enter below the message').pack() #getting error here
enter_mess_here = tk.Entry(root).pack() #getting error here
def getting_message():
message_here = enter_mess_here.get()
print(message_here)
done_procces_mess = tk.Label(root, text = "done!").pack() #getting error here
get_mess = tk.Button(root, text = "procces message", command = getting_message).pack() #getting error here

root.mainloop()

您的问题是您在变量enter_mess_here中设置了None:

enter_mess_here = tk.Entry(root).pack() #getting error here

,在你的函数中,当你想要获得值,它不是从Entry中获得值,而是从Nothing中获得值。所以它会抛出一个错误

首先将Entry分配给一个变量,然后使用pack,这样就可以工作了:

enter_mess_here = tk.Entry(root)
enter_mess_here.pack()

最新更新