Home > database >  How to display the output form a function on a label in Tkinter
How to display the output form a function on a label in Tkinter

Time:11-22

I'm new to coding and im sure that my code is not very efficient but I just want to take the output from a variable and display it in a window. So far when you run it, it just displays the output in the console. I want it do to that and display it on the window. Hope that all makes sense.

from tkinter import *
root = Tk()

root.geometry("500x500")
def get_input():
    year = boxYear.get()
    p1 = (int(year) // 12)
    p2 = (int(year) % 12)
    p3 = (p2 // 4)
    p4 = (p1   p2   p3)
    days = ['wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'monday', 'tuesday']
    p5 = (p4 // 7)
    if p4 >= 7 and p4 <= 14:
        p6 = (p4 - 7)
    elif p4 >= 7 and p4 > 14:
        p6 = (p4 - 14)
    else:
        p6 = (p4)
    if p6 == 7:
        p6 = 0
    print(days[int(p6)])

    
# in between these two sections there is a bunch of code that's just math. its not important.
# it just spits out a variable which is one of the days of the week
#the variable "final" is that day of the week 



    final = int(last)
    DOTW = (days[int(final)])
    outputlabel = Label(topframe, textvariable=DOTW, font=('Arial 20 bold italic'))
    outputlabel.grid(row=7, columnspan=2, pady=10)





#GUI stuff
topframe = Frame(root)
topframe.pack()
bottomframe = Frame(root)
bottomframe.pack(side=BOTTOM)

printbutton = Button(topframe, text="Run Algorithm", command=lambda: get_input())
printbutton.grid(row= 5, columnspan=2, pady=30)

boxYear = Entry(topframe)
boxMonth = Entry(topframe)
boxDay = Entry(topframe)

boxYear.grid(row=1, column=1, padx=10, pady=10)
boxMonth.grid(row=2, column=1, padx=10, pady=10)
boxDay.grid(row=3, column=1, padx=10, pady=10)

root.mainloop()

I tried to add code to get it to display it in the window but it just doesn't do anything and I can't find a solution anywhere.

CodePudding user response:

For starters, your get_input function isn't returning a value. You should replace the print() statement with the value you'd like this function to return:

def get_input():
    ...  # code omitted for brevity
    return days[int(p6)]  # return the value you want from the `days` list

That said, if you want this function to update a UI element like a Label, the preferred way to handle that is as follows:

root = Tk()
# you were close - the Label's textvariable should be an instance of StringVar
dotw_var = StringVar(root)
# if you want 'dotw_var' (and consequently, 'outputlabel') to have a default value,
# declare dotw_var as follows: dotw_var(root, 'default value string')

outputlabel = Label(topframe, textvariable=dotw_var, font=('Arial 20 bold italic'))
outputlabel.grid(row=7, columnspan=2, pady=10)


def get_input():
    ...  # code omitted for brevity
    dotw_var.set(days[int(p6)])  # set the value to update outputlabel
    # note that you don't need this function to return anything after all!

Whenever dotw_var is set(), the text of outputlabel will update automatically

CodePudding user response:

You have used textvariable=DOTW for the outputlabel, as DOTW is a string, an internal tkinter variable will be created with empty string initially, so the label shows a empty string.

You should use text=DOTW instead. Also it is better to create outputlabel once outside the function and update its text inside the function.

Below is the required changes:

def get_input():
    ...
    final = int(last)
    DOTW = days[final] # don't need to call int() on final as it is already an integer
    outputlabel.config(text=DOTW)  # update outputlabel

...

# create outputlabel initially
outputlabel = Label(topframe, font=('Arial 20 bold italic'))
outputlabel.grid(row=7, columnspan=2, pady=10)

root.mainloop()
  • Related