简体   繁体   中英

How can I make it so the text boxes in tkinter are created on a canvas and not on the window itself?

I am trying to make it so whenever you press the "+" button, it creates a text box on the canvas that is inside of the window. But, whenever I try to do that, it just puts it on the window itself and not on the canvas. Here is the code. Any help would be greatly appreciated!

from tkinter import *
from random import choice
root = Tk()
    


root.title("TBCITW")
root.geometry('340x800')
root.resizable (1,1)

canvasmain=Canvas(
    root,
    bg="#1c1c1c",
    width=340, 
    height=10000,)
canvasmain.place(x=-2, y=41,)
    
def plusentrymain():
    global textplus
    textplus=Entry(canvasmain).grid(padx=5, pady=5)
    

#Buttons
button1=Button(root, text=" + ", padx=2,command=plusentrymain)
button1.place(x=10, y=10)

root.mainloop()

I changed the widget canvasmain to root in line 21. Outside of the function, I added an Entry.

Python is the rule. I altered the code to make it readable.

from tkinter import *
from random import choice


root = Tk()
root.title("TBCITW")
root.geometry('340x800')
root.resizable (1,1)

canvasmain=Canvas(
    root,
    bg="#1c1c1c",
    width=340, 
    height=10000,
    )

canvasmain.place(x=2, y=41,)
    
def plusentrymain():
    global textplus
    textplus=Entry(root)
    textplus.grid(padx=5, pady=5)
    

#Buttons
button1=Button(root, text=" + ", padx=2, command=plusentrymain)
button1.place(x=10, y=10)

#Entry
textplus=Entry(root)
textplus.grid(padx=5, pady=45)

root.mainloop()

Output:

Before click..

在此处输入图像描述

After click.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM