1

I have a GUI with two entry boxes: one for "miles at last oil change" and one for "current miles". There's a button which, upon clicked, runs a function that reads input from the entry boxes and prints how many miles I am due or past due, etc. I want to display the output to the GUI. I know I have to create a label widget but how do I make it so that when I press the button and the function gets called, the label text gets updated with info from the function?

from tkinter import *
from tkinter import ttk


#initializing root window
root = Tk()
root.title("Car Maintenance App")

#functions
miles = IntVar()
last_miles = IntVar()

def check_oil_change():
    miles = miles_entry.get()
    miles = int(miles)
    last_miles = lastmiles_entry.get()
    last_miles = int(last_miles)
    miles_till_oilchange = 3000 - (miles - last_miles)
    if miles_till_oilchange == 0:
        print("You are due for an oil change")
    if miles_till_oilchange > 0:
        print("You have {} miles until next oil change.".format(miles_till_oilchange))
    if miles_till_oilchange < 0:
        print("You are over due {} miles for an oil change.".format(abs(miles_till_oilchange)))

#creating container for widgets
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

#widgets
milesLabel = ttk.Label(mainframe, text= "Enter your cars current mileage:")
lastmilesLabel = ttk.Label(mainframe, text= "How many miles was your last oil change at?")

miles_entry = ttk.Entry(mainframe, width=7)
lastmiles_entry = ttk.Entry(mainframe, width=7)

milesButton = ttk.Button(mainframe, text="Enter", command=check_oil_change)

#positioning
milesLabel.grid(row=1, column=0)
miles_entry.grid(row=1, column=1)
milesButton.grid(row=1, column=2)
lastmilesLabel.grid(row=0, column=0)
lastmiles_entry.grid(row=0, column=1)

root.mainloop()
2
  • Hello and welcome to StackOverflow! On a side note, I notice that you're using both snake casing and camel casing. I would pick one (or, say, use camel casing for functions and snake casing for everything else). Commented Jul 25, 2021 at 17:51
  • @DanielWalker Thank you! I'll probably switch to using snake casing exclusively. I don't know why I used both. Commented Jul 25, 2021 at 18:24

1 Answer 1

1

In that case, you casn define a label, and use .config() method.

.config() method allows you to configure the specified widget. You can edit any parameters of the widget.

from tkinter import *
from tkinter import ttk

#initializing root window
root = Tk()
root.title("Car Maintenance App")

#functions
miles = IntVar()
last_miles = IntVar()

def check_oil_change():
    miles = miles_entry.get()
    miles = int(miles)
    last_miles = lastmiles_entry.get()
    last_miles = int(last_miles)
    miles_till_oilchange = 3000 - (miles - last_miles)

    if miles_till_oilchange == 0:
        mile_lbl.config(text="You are due for an oil change")

    elif miles_till_oilchange > 0:
        mile_lbl.config(text="You have {} miles until next oil change.".format(miles_till_oilchange))

    elif miles_till_oilchange < 0:
        mile_lbl.config(text="You are over due {} miles for an oil change.".format(abs(miles_till_oilchange)))



#creating container for widgets
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

#widgets
milesLabel = ttk.Label(mainframe, text= "Enter your cars current mileage:")
lastmilesLabel = ttk.Label(mainframe, text= "How many miles was your last oil change at?")

miles_entry = ttk.Entry(mainframe, width=7)
lastmiles_entry = ttk.Entry(mainframe, width=7)

milesButton = ttk.Button(mainframe, text="Enter", command=check_oil_change)
mile_lbl=Label(mainframe,font=("arial","bold"))

#positioning
milesLabel.grid(row=1, column=0)
miles_entry.grid(row=1, column=1)
milesButton.grid(row=1, column=2)
mile_lbl.grid(row=2,column=0)
lastmilesLabel.grid(row=0, column=0)
lastmiles_entry.grid(row=0, column=1)

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.