Relatively new to coding in general, and have been trying to create a simple chat client for Python. Trying to work the GUI, and have encountered the problem shown below.

I have a functioning GUI and use the "example.get()" function to retrieve the string text from an entry box. The program then prints the text to the command prompt (Just to prove it's been retrieved) and then should place it in a text box, however it gives me a "Nonetype" error. Code is below. Does anyone have any idea how to fix this?

Thanks

from tkinter import *

#Create GUI
root=Tk()
root.title("Chat test")
root.geometry("450x450+300+300")

#Declare variables
msg=StringVar()

#Get and post text to chat log
def postaction():
    msg1=msg.get()
    print(msg1)
    chatlog.insert(INSERT,msg1+'\n')
    root.mainloop()

#Build GUI components
chatlog=Text(root, height=10, state=DISABLED).pack(side=TOP, fill=X)
entry=Entry(root, textvariable=msg).pack(side=BOTTOM, fill=X)
button=Button(root, command=postaction, text="Button").pack()
有帮助吗?

解决方案

The .pack method of a widget always returns None. So, you need to place the calls to .pack on their own line:

chatlog=Text(root, height=10, state=DISABLED)
chatlog.pack(side=TOP, fill=X)
entry=Entry(root, textvariable=msg)
entry.pack(side=BOTTOM, fill=X)
button=Button(root, command=postaction, text="Button")
button.pack()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top