0

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()
Matiiss
  • 5,970
  • 2
  • 12
  • 29
Laxmi
  • 21
  • 8
  • 1
    Show us the code that you are using, so we dont make any wrong assumptions. – Delrius Euphoria Apr 13 '21 at 08:12
  • I see the issue: `entb = ttk.Entry(win).grid(row=1, column=2)` this, if You assign a variable to the class and then use a method on it immediately it won't work. You have to do `entb = ttk.Entry(win)` and then: `entb.grid(row=1, column=2)` the next line. and also You have to add `.get()` in the function definition like this: `mbox.showinfo(entb.get(), entb1.get())` – Matiiss Apr 13 '21 at 08:24
  • dude, why are You not accepting the edit, I just added ``` to format the code and You are removing that leaving the post looking worse (at least I think it is You) – Matiiss Apr 13 '21 at 08:26
  • thank you so much for your help. Actually, I am new to stack overflow so I tried to post the message and code separately but I am unable to do so I put the code and message like that only. – Laxmi Apr 13 '21 at 15:02

2 Answers2

0

The working version of your code:

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

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

# It is basically useless to store the return value of the 'grid' function in a variable since it returns nothing
# You should make an object of the widget NOT the widget's grid function. Also 'grid' rows and columns start at 0 not 1
leb = ttk.Label(win, text='Enter the 1st details')
leb.grid(row=0, column=0)
leb2 = ttk.Label(win, text='Enter the 2nd details')
leb2.grid(row=1, column=0)

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


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


btn = ttk.Button(win, text='Show', command=show)
btn.grid(row=2, column=1)
# Also if you want to destroy your GUI it is better to use 'win.destroy()' instead of 'exit()'
btn1 = ttk.Button(win, text='Exit', command=win.destroy)
btn1.grid(row=2, column=2)

win.mainloop()

The reason that your code was not working was that you were setting the variable to the grid function of the widget instead of the actual widget. The grid function bascially returns nothing so that is why when you put the variables in the function nothing showed up. It is always better to do it as the method above unless you want to make something that you won't configure during the entire program. Also two side notes:

  • The grid functions's rows and columns start at 0 not 1
  • Whenever you want to end your graphical user interface, you should use 'win.destroy()' instead of 'exit()' because exit would end the entire program with the GUI
Omid Ki
  • 155
  • 3
  • 13
  • thank you so much. – Laxmi Apr 13 '21 at 15:03
  • @Laxmi your welcome. Since you are new to stckoverflow in order to mark an answer as the chosen one so that others know what worked (at least for you) you have to click the check mark next to the post. Glad I could help! – Omid Ki Apr 13 '21 at 15:17
0

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()
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sheik-Yabouti
  • 85
  • 1
  • 9