Error showing variable in messagebox

1

I am doing a unit converter and I would like to press the button to show the number that I have written in the text box in messagebox , but it does not work. The messagebox always shows no content no matter what is entered in Entry .

This is my code:

from tkinter import*
import tkinter as tk
from string import*
from tkinter import messagebox


def Conversor():
    global numero
    root = tk.Tk()
    root.title("Conversor Km a Mi")
    root.geometry("280x70+100+50")
    tk.Label(text='Conversor',fg='black',font=10) .pack()
    KMDATA = tk.Entry()
    KMDATA.pack(fill=X)
    numero = KMDATA.get()
    tk.Button(root, text='Convertir',fg="black",font=12, bg="light 
    green", command=varconv) .pack(side = BOTTOM, fill=X)

    root.mainloop()

def varconv():
    messagebox.showinfo("Resultado", numero)


Conversor()
    
asked by Alberto Dos Santos Rodriguez 12.03.2018 в 12:13
source

1 answer

0

The problem is on the line:

numero = KMDATA.get()

This causes at the time you execute the function the variable numero to have the value of the Entry ( "" at that moment). When you enter text in the Entry this variable does not change, then the MessageBox always shows an empty string.

There are many ways to solve this by keeping your code within the function, basically you must get the content of the Entry at the same time when the button is pressed and not at the beginning of the program:

  • Make KMDATA (or a tkinter.StringVar associated with it) be global and use get within the function that creates the MessageBox :

    import tkinter as tk
    from tkinter import messagebox
    
    def conversor():
        global numero
        root = tk.Tk()
        root.title("Conversor Km a Mi")
        root.geometry("280x70+100+50")
        tk.Label(root, text='Conversor', fg='black', font=10).pack()
        numero = tk.StringVar(root)
        tk.Entry(root, textvar=numero).pack(fill=tk.X)
        tk.Button(root, text='Convertir', fg="black", font=12,
                  bg="light green", command=varconv).pack(side=tk.BOTTOM, fill=tk.X)
        root.mainloop()
    
    
    def varconv():
        messagebox.showinfo("Resultado", numero.get())
    
    conversor()
    
  • Dispense with the use of global variables and pass the reference of Entry or StringVar as an argument (via lambda or functools.parial ):

    import tkinter as tk
    from functools import partial
    from tkinter import messagebox
    
    def conversor():
        root = tk.Tk()
        root.title("Conversor Km a Mi")
        root.geometry("280x70+100+50")
        tk.Label(root, text='Conversor', fg='black', font=10).pack()
        numero = tk.StringVar(root)
        tk.Entry(root, textvar=numero).pack(fill=tk.X)
        tk.Button(root, text='Convertir', fg="black", font=12, bg="light green", 
                  command=partial(varconv, numero)).pack(side=tk.BOTTOM, fill=tk.X)
        root.mainloop()
    
    
    def varconv(var):
        messagebox.showinfo("Resultado", var.get())
    
    
    conversor()
    
  • You can define varconv directly within the function conversor :

    import tkinter as tk
    from tkinter import messagebox
    from functools import partial
    
    def conversor():
    
        def varconv():
            messagebox.showinfo("Resultado", numero.get())
    
        root = tk.Tk()
        root.title("Conversor Km a Mi")
        root.geometry("280x70+100+50")
        tk.Label(text='Conversor', fg='black', font=10).pack()
        numero = tk.StringVar(root)
        tk.Entry(root, textvar=numero).pack(fill=tk.X)
        tk.Button(root, text='Convertir', fg="black", font=12,
                  bg="light green", command=varconv).pack(side=tk.BOTTOM, fill=tk.X)
        root.mainloop()
    
    
    
    conversor()
    

There are many other possibilities to use POO:

import tkinter as tk
from tkinter import messagebox

class Conversor(tk.Tk):
    def __init__(self):
        super(Conversor, self).__init__()
        self.title("Conversor Km a Mi")
        self.geometry("280x70+100+50")

        self.numero = tk.StringVar(self)

        tk.Label(self, text='Conversor', fg='black', font=10).pack()
        tk.Entry(self, textvar=self.numero).pack(fill=tk.X)
        tk.Button(self, text='Convertir', fg="black", font=12, bg="light green",
                  command=self.var_conv).pack(side=tk.BOTTOM, fill=tk.X)

    def var_conv(self):
        try:
            ent = float(self.numero.get())
            res = "{} mi".format(ent * 0.62137)
        except:
            res = "El valor introducido no es numérico"

        messagebox.showinfo("Resultado", res)

if __name__ == "__main__":
    conv = Conversor()
    conv.mainloop()
    
answered by 12.03.2018 / 13:12
source