Search code examples
pythonpython-3.xtkintertkinter-entry

how to show the user's input in tkinter using message box?


How to show the user's input in Tkinter using a message box with the title of the message box? I am using the get method that is not working and use the normal method by passing a two-variable name in show info that is also not working. below is the code which I am using.

import tkinter as tk
from tkinter import ttk
import tkinter.messagebox as mbox

win = tk.Tk()
win.title('Pratice')

leb = ttk.Label(win, text='Enter the 1st details').grid(row=1, column=1)
leb2 = ttk.Label(win, text='Enter the 2nd details').grid(row=2, column=1)


entb = ttk.Entry(win).grid(row=1, column=2)
entb1 = ttk.Entry(win).grid(row=2, column=2)


def show():
    mbox.showinfo(entb, entb1)


btn = ttk.Button(win, text='Show', command=show).grid(row = 3, column = 1, columnspan=4)
btn1 = ttk.Button(win, text='Exit', command=exit).grid(row = 3, column = 3, columnspan=3)

win.mainloop()

Solution

  • Like Matiiss stated if you try to implement a method to a class, right after assigning a variable to a class it will not work. Following Matiiss' suggestion in the edit I believe this code will serve your purposes.

    It isn't pretty, but it'll work. I would like to suggest that you look into the basics of how to use classes in python, it'll help keep your code neat and easier to interpret if someone read your code.

    import tkinter as tk
    from tkinter import ttk
    import tkinter.messagebox as mbox
    
    win = tk.Tk()
    win.title('Pratice')
    
    leb = ttk.Label(win, text='Enter the 1st details').grid(row=1, column=1)
    leb2 = ttk.Label(win, text='Enter the 2nd details').grid(row=2, column=1)
    
    
    entb = ttk.Entry(win)
    entb.grid(row=1, column=2)
    entb1 = ttk.Entry(win)
    entb1.grid(row=2, column=2)
    
    
    def show():
        mbox.showinfo(entb.get(), entb1.get())
    
    
    btn = ttk.Button(win, text='Show', command=show).grid(row = 3, column = 1, columnspan=4)
    btn1 = ttk.Button(win, text='Exit', command=exit).grid(row = 3, column = 3, columnspan=3)
    
    win.mainloop()