I want to print the result of my entry when clicking the button how do I make it work?

39 Views Asked by At
def mdp():
    new3 = Toplevel(win)
    window_height = 160
    window_width = 300
    screen_width = new3.winfo_screenwidth()
    screen_height = new3.winfo_screenheight()
    x_cordinate = int((screen_width / 2) - (window_width / 2))
    y_cordinate = int((screen_height / 2) - (window_height / 2))
    new3.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
    text1 = ttk.Label(new3, text="MOT DE PASSE:")
    text1.place(x=50, y=30)
    entry1 = Entry(new3, show="*")
    entry1.place(x=50, y=50, width=200, height=20)
    b1 = Button(new3, text="CONFIRMER", command=mdpcheck)
    b1.place(x=100, y=80, width=100, height=20)
    return entry1


def mdpcheck():
    entry1 = mdp()
    réponse2 = entry1.get()
    print(réponse2)

I tried getting the entry value and then returning a variable, but it would return me the value when the function has been started so nothing

1

There are 1 best solutions below

0
On

On line 18, add keyword textvariable entry1 = Entry(new3, show="*", textvariable=_var)

On line 25, add _var = StringVar()

Snippet:

from tkinter import *
from tkinter import ttk

win =Tk()
win.withdraw()

def mdp():
    new3 = Toplevel(win)
    window_height = 160
    window_width = 300
    screen_width = new3.winfo_screenwidth()
    screen_height = new3.winfo_screenheight()
    x_cordinate = int((screen_width / 2) - (window_width / 2))
    y_cordinate = int((screen_height / 2) - (window_height / 2))
    new3.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
    text1 = ttk.Label(new3, text="MOT DE PASSE:")
    text1.place(x=50, y=30)
    entry1 = Entry(new3, show="*", textvariable=_var)
    entry1.place(x=50, y=50, width=200, height=20)
    b1 = Button(new3, text="CONFIRMER", command=mdpcheck)
    b1.place(x=100, y=80, width=100, height=20)
    return entry1


_var = StringVar()
def mdpcheck():
    entry1 = mdp()
    réponse2 = entry1.get()
    print(réponse2)

    
mdpcheck()
win.mainloop()

Screenshot:

enter image description here