Answering the second part of your question first: The pack()
method always returns None
, so your code what assigning that value to all the widgets except the Canvas
.
To get the text when the Submit is pressed you need to define a callback function and then pass its name as the command=
keyword argument when you create the Button
. In the code below this function has been named submit()
.
To get text from a Text
widget, you need to call its get()
method and pass it two indices. To get all the text use get("1.0", END)
. This is done in the function and the retrieved text is printed.
from tkinter import *
main = Tk()
width = 600
height = 600
def submit(): # Callback function for SUBMIT Button
text = textbox.get("1.0", END) # For line 1, col 0 to end.
print(f'{text=!r}')
c = Canvas(main, width=width, height=height, highlightthickness=0)
c.pack()
submitbutton = Button(c, width=10, height=1, text='SUBMIT', command=submit)
submitbutton.pack()
textbox = Text(c, width=30, height=2)
textbox.pack()
tboxlabel = Label(c, text='label')
tboxlabel.pack()
quitbutton = Button(c, width=10, height=1, text='QUIT', command=quit)
quitbutton.pack()
mainloop()
Here's a screenshot of what it looks like running on my system. When the Submit is clicked, it will retrieve the contents of the Text
widget and print it out.
