0

The title says it all. I have looked about for fixes and tried implementation of them but it seems to not work on my code.

Also, what is the difference to .pack() when you declare the widget/variable or on the next line?

from tkinter import *
import addition

main = Tk()

width = 600
height = 600

c = Canvas(main, width=width, height=height)
c.pack()

submitbutton = Button(c, width=10, height=1, text='SUBMIT').pack()

textbox = Text(c, width=30, height=2).pack()
tboxlabel = Label(textbox, text='label').pack()

quitbutton = Button(c, width=10, height=1, text='QUIT', command=quit).pack()

mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
Fin HARRIS
  • 118
  • 10
  • Your code does not have any stuff related to getting input from textbox. For line like `textbox = Text(...).pack()`, `textbox` is assigned the result of `pack()` (which is None), not `Text(...)`. So `textbox` will be None instead of an instance of `Text()`. – acw1668 Jan 25 '21 at 01:24

1 Answers1

0

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.

screenshot of script running

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Normally `pack() / grid() / place()` is not used to put widgets into `Canvas`. Also `c.create_window(100, 100, window=tboxlabel)` will be override by `tboxlabel.pack()`. – acw1668 Jan 25 '21 at 02:15
  • @acw1668: Agree the coordinates don't matter, however without the `tboxlabel.pack()` ,the `Label` is invisible. – martineau Jan 25 '21 at 02:24
  • It is not invisible, it is put in the upper left corner of the window (only bottom right part is visible). If you use `anchor="nw"` in `.create_window(...)`, you will see the whole label. – acw1668 Jan 25 '21 at 02:27
  • @acw1668: A-ha, now I get it. Thanks for the clarification. However the point's now moot since I've revised my answer again and it is no longer using the `Canvas.create_window()` function (and just calls `pack()` like the OP does with the other widgets). I didn't know a `Canvas` could more-or-less be used just like a `Frame` — at least not to the degree it is being done here. – martineau Jan 25 '21 at 10:13
  • 1
    Yes you can use a `Canvas` like a container, but using `pack / place / grid` will not make the widgets to be scrollable (i.e. their position will not be changed if the canvas is being scrolled). Also they are not included in the calculation of `scrollregion` – acw1668 Jan 25 '21 at 10:20
  • @acw1668: Given that, it seems relatively pointless to use a `Canvas` in conjunction with one those layout manager methods and the only things on it are going to be regular tkinter widgets (i.e. as opposed to other `Canvas`-only items like lines, ovals, polygons, etc. — or widgets inside "window" items). – martineau Jan 25 '21 at 11:50