Home > Net >  How to display on TKinter what a function returns?
How to display on TKinter what a function returns?

Time:05-04

I would like to display in the window what a function returns in Tkinter text. But I can't do it. Can you help me please

Here is my code (there are no errors, but nothing is displayed):

from tkinter import *


fen = Tk()
fen.geometry('500x500')

a=10

def calcul(a):
    b=a*10
    return b

def DoCalcul():
    calcul(a)

text1=Label(fen, Text=DoCalcul(), width=80, height=2, fg="green", font=('arial', 13))
text1.pack()



fen.mainloop()

CodePudding user response:

You aren't returning a value from the DoCalcul function. Also you had used Text rather than text as a parameter keyword for Label

See below code

from tkinter import *


fen = Tk()
fen.geometry('500x500')

a=10

def calcul(a):
    b=a*10
    return b

def DoCalcul():
    return calcul(a)

text1=Label(fen, text=f"Result is {DoCalcul()}", width=80, height=2, fg="green", font=('arial', 13)) 
text1.pack()



fen.mainloop()



fen.mainloop()

I've also updated the code to show how to add additional text using python f-strings.

CodePudding user response:

The quick solution is to add a return to your function:

def DoCalcul():
    return calcul(a)

Another issue is that you should explicitly call the global variable a. Python does eventually check global if it cannot find the variable in the local namespace but this may cause issues down the road as your code expands so instead add the global reference.

def DoCalcul():
    global a
    return calcul(a)

That said Its not common to use a function call in a text field so lets look at some other options.

  1. Instead of importing * you can do import tkinter as tk. This servers 2 purposes. One is so we do not override any existing imported methods/functions/classes ect and to make it easier to ID where we are using tkinter related methods.
  2. For small calculations like this you can write it into the text field directly or you can use a StringVar()/IntVar() to hold a dynamically changing value if you need to keep your label updating.
  3. You have a typo in your label. it is text and not Text.

You use 2 functions to do what one function can do just fine if you insist on using a function call in your label directly:

import tkinter as tk


fen = tk.Tk()
fen.geometry('500x500')
a = 10


def calcul(a):
    b=a*10
    return b


text1 = tk.Label(fen, text=calcul(a), width=80, height=2, fg="green", font=('arial', 13))
text1.pack()

To run the calculation in the label directly you can do this:

import tkinter as tk


fen = tk.Tk()
fen.geometry('500x500')
a = 10

text1 = tk.Label(fen, text=a*10, width=80, height=2, fg="green", font=('arial', 13))
text1.pack()

fen.mainloop()

Using a StringVar() or IntVar() here you can do this:

import tkinter as tk


fen = tk.Tk()
fen.geometry('500x500')
a = 10

label_var = tk.StringVar(fen)
label_var.set(a*10)

text1 = tk.Label(fen, text=label_var.get(), width=80, height=2, fg="green", font=('arial', 13))
text1.pack()

fen.mainloop()

Edit:

To answer your question about concatenation in the comments you can try one of the following 3 methods if to do that.

.format() the use of or f strings:

import tkinter as tk


fen = tk.Tk()
fen.geometry('500x500')
a = 10

label_var = tk.StringVar(fen)
label_var.set(a*10)

tk.Label(fen, text="The result is: "   label_var.get(), width=80, height=2, fg="green", font=('arial', 13)).pack()
tk.Label(fen, text="The result is: {}".format(label_var.get()), width=80, height=2, fg="green", font=('arial', 13)).pack()
tk.Label(fen, text=f"The result is: {label_var.get()}", width=80, height=2, fg="green", font=('arial', 13)).pack()


fen.mainloop()
  • Related