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()
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.