How to place a text ‘object’ dynamicaly at the end of the canvas in Python

I am (trying) learning to program Python, so my question probably is very basic. But at this moment a challenge for me..As a learning case I want to create a user interface for a chatbot. But I do not only want to put text in the scrollable userinterface, but also (interactive) buttons.

I learned that I need to use a canvas to make buttons scrollable. This basic mechanism works. My question is how to add text in the scrollable canvas at the end. Using a frame the text can be added using…but how do I position text after I have “inserted” the buttons on the canvas ?

I have tried to get the y position but that seems to give a fixed position.

The position of the text should also work if the “canvas” starts scrolling.

my code sofar:

import tkinter
from tkinter import *


win=Tk()
wrapper1 = LabelFrame(win)

mycanvas = Canvas(wrapper1)
mycanvas.pack(side=LEFT, fill="both" , expand="yes")

yscrollbar = Scrollbar(wrapper1, orient="vertical", command=mycanvas.yview)
yscrollbar.pack(side=RIGHT, fill="y")

mycanvas.configure(yscrollcommand=yscrollbar.set)
mycanvas.bind('<Configure>', lambda e: mycanvas.configure(scrollregion=mycanvas.bbox('all')))

myFrame = Frame(mycanvas)
mycanvas.create_window((0,0), window=myFrame, anchor="nw")

wrapper1.pack(fill="both", expand="yes", padx=10 , pady=10)

Button1 = Button( myFrame , text="Button 1")
Button1.pack()
widgety = Button1.winfo_rooty()
print(widgety)

Button2 = Button( myFrame , text ="Button 2")
Button2.pack()
widgety = Button2.winfo_rooty()
print(widgety)

mycanvas.create_text(5,340, text="test",anchor=SW)

win.title("testing screen")
win.geometry("500x500")
win.resizable(False,False)
win.mainloop()

I have tried create_text method but that requires a fixed position. I have tried to get the y position dynamicaly ….

  • Can you clarify your question? What do you mean by “The position of the text should also work if the “canvas” starts scrolling”?

    – 

  • Apologies for being unclear. I mean that in the chatbot the conversation will not be limited to the 2 buttons in my example. But the amount of lines of text and/or buttons will increase. And than the latest text should still be added to the end..hope this make it more clear

    – 

  • Is the use of Label also applicable of the text is longer (as it is the conversation text in a chatbot) ?

    – 

Leave a Comment