Ask the Python GUI about utilizing the entry.

Asked 2 years ago, Updated 2 years ago, 14 views

from tkinter import*
from tkinter.colorchooser import*

def paint():
    color = askcolor()
    rec = Tk()
    r = Canvas(rec, width= 100, height = 100)
    r.pack()

    r.create_rectangle(10,10,90,90, fill = color[1])



def draw():
    drawing = Tk()
    c = Canvas(drawing, width = 500, height = 500, background="green")
    c.pack()

    c.create_polygon(e0,e1,e2,e3,e4,e5, fill="blue")    

def seconde():
    setting = Tk()

    Label(setting, text="x1").grid(row=0)
    Label(setting, text="y1").grid(row=1)
    Label(setting, text="x2").grid(row=2)
    Label(setting, text="y2").grid(row=3)
    Label(setting, text="x3").grid(row=4)
    Label(setting, text="y3").grid(row=5)

    e0 = Entry(setting).grid(row=0,column=1)
    e1 = Entry(setting).grid(row=1,column=1)
    e2 = Entry(setting).grid(row=2,column=1)
    e3 = Entry(setting).grid(row=3,column=1)
    e4 = Entry(setting).grid(row=4,column=1)
    e5 = Entry(setting).grid(row=5,column=1)


    set1 = Button (setting, text = "set" , command = draw).grid(row=6,column=1)


canvers = Tk()

start = Button (canvers, text = "POLYGON", command = second)
start2 = Button (canvers, text = "RecT"), command = paint)

start.pack()
start2.pack() #First

canvers.mainloop()

In the code above, I want to create a polygon by setting coordinates using the e with Entry, but it doesn't work as I thought.

I set it as a global variable just in case, but there was an error.

What should I do?

python

2022-09-21 17:56

1 Answers

It's working like this.



def draw(entries):

    drawing = Tk()
    c = Canvas(drawing, width = 500, height = 500, background="white")
    c.pack()

    e = [ entries[i].get() for i in range(6) ]
    print(e)
    c.create_polygon(e[0],e[1],e[2],e[3],e[4],e[5], fill="blue")    

def seconde():

    setting = Tk()

    Label(setting, text="x1").grid(row=0)
    Label(setting, text="y1").grid(row=1)
    Label(setting, text="x2").grid(row=2)
    Label(setting, text="y2").grid(row=3)
    Label(setting, text="x3").grid(row=4)
    Label(setting, text="y3").grid(row=5)

    entries = [ Entry(setting) for _ in range(6) ]
    [ e.grid(row=i, column=1) for e, i in zip(entries, range(6)) ]
    print(entries)

    Button (setting, text="setting"; command = lambda : draw (entries).grid(row=6,column=1)


2022-09-21 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.