Currently, I am doing a project trying to make a weather recommendation app. My current dilemma is that when I execute the app the window pops up as intended but, when I try to insert a reset button to delete the entries a user inputs it overlaps the convert button. I’ve been using the grid()function instead of the pack(). I am also having an issue with pressing the previous and next buttons as the entry box moves along with it when pressed. I want the entry box and in the future the result box to only be on page 2 instead of every page.
I have attempted to solve the issue by using columns and rows, as well as the sticky option, but unfortunately, I was not able to find a solution. My objective is to have the reset button and convert button placed next to each other. As well as the entry box for the user inputted temperature to only remain on page 2 along with the result label I plan on making.
import tkinter as tk
class Myapp(tk.Frame):
def __init__(self, root):
self.current_page_index = 0
self.pages = [self.page1, self.page2, ]#self.page3]
self.color1 = '#95d6ea'
self.color2 = '#62a1c7'
self.color3 = 'White'
super().__init__(
root,
bg = self.color1
)
self.main_page = self
self.main_page.pack(fill = tk.BOTH, expand = True)
self.main_page.columnconfigure(0, weight = 1)
self.main_page.rowconfigure(0, weight = 1)
self.load_main_widgets()
#Defines the formula to convert Celsius to Fahrenheit
def Celsius_to_Fahrenheit(Celsius):
return (1.8) * Celsius + 32
#Defines the formula to convert Fahrenheit to Celsius
def Fahrenheit_to_Celsius(Fahrenheit):
return (0.55) * (Fahrenheit - 32)
#The float class is used so the user is able to input decimial numbers, an uses the .2f so the output is rounded up two decimal places.
#def convert_temperature():
try:
temperature = float(entry.get())
if temperature_type.get() == "Celsius":
result = Celsius_to_Fahrenheit(temperature)
result_label.config(text =f"{temperature} Celsius is {result:.2f} Fahrenehit")
else:
result = Fahrenheit_to_Celsius(temperature)
result_label.config(text = f"{temperature} Fahrenheit is {result:.2f} Celsius")
except ValueError:
result_label.config(text = "Invalid User input. Please enter a valid number.")
def load_main_widgets(self):
self.create_page_container()
self.create_pager()
self.pages[self.current_page_index]()
def clear_frame(self, frame):
for child in frame.winfo_children():
child.destroy()
def create_page_container(self):
self.page_container = tk.Frame(
self.main_page,
background = self.color1
)
self.page_container.columnconfigure(0, weight = 1)
self.page_container.rowconfigure(0, weight = 1)
self.page_container.rowconfigure(1, weight = 1)
self.page_container.grid(column = 0, row = 0, sticky = tk.NSEW)
def create_pager(self):
self.pager = tk.Frame(
self.main_page,
background = self.color1,
height = 125,
width = 400,
)
self.pager.columnconfigure(1, weight = 1)
self.pager.rowconfigure(0, weight = 1)
self.pager.grid(column = 0, row = 1, sticky = tk.NS)
self.pager.grid_propagate(0)
def change_page(button):
self.clear_frame(self.page_container)
match button:
case "Previous":
self.current_page_index -= 1
self.pages[self.current_page_index]()
case "Next":
self.current_page_index += 1
self.pages[self.current_page_index]()
if self.current_page_index == 0:
previous_button.config(state =tk.DISABLED)
else:
previous_button.config(state = tk.ACTIVE)
if self.current_page_index == len(self.pages) - 1:
next_button.config(state =tk.DISABLED)
else:
next_button.config(state =tk.ACTIVE)
self.page_number["text"] = f'{self.current_page_index + 1}/{len(self.pages)}'
previous_button = tk.Button(
self.pager,
background = self.color2,
foreground = self.color3,
activebackground = self.color2,
activeforeground = self.color3,
disabledforeground = '#3B3A56',
highlightthickness = 0,
width = 7,
relief = tk.FLAT,
font = ("Comic Sans", 10),
text = "Previous",
state = tk.DISABLED,
command = lambda button = "Previous" :change_page(button)
)
previous_button.grid(column = 0, row = 0, sticky = tk.SW)
self.page_number = tk.Label(
self.pager,
background = self.color1,
foreground = self.color3,
font = ("Comic Sans", 10),
text = f'{self.current_page_index + 1}/{len(self.pages)}'
)
self.page_number.grid(column = 1, row = 0)
next_button = tk.Button(
self.pager,
background = self.color2,
foreground = self.color3,
activebackground = self.color2,
activeforeground = self.color3,
disabledforeground = '#3B3A56',
highlightthickness = 0,
width = 7,
relief = tk.FLAT,
font = ("Comic Sans", 10),
text = "Next",
command = lambda button = "Next" :change_page(button)
)
next_button.grid(column = 2, row = 0, sticky = tk.SE)
def page1(self):
title = tk.Label(
self.page_container,
background = self.color1,
foreground = self.color3,
height = 2,
font = ("Comic Sans", 25),
text = "Welcome!"
)
title.grid(column = 0, row = 0)
text = ("Hello, and welcome to my weather recommendation app! "
"To continue to the application please click next or click to exit window.")
content = tk.Label(
self.page_container,
background = self.color2,
foreground = self.color3,
justify = tk.LEFT,
anchor = tk.N,
pady = 20,
font = ("Comic Sans", 22),
text = text,
wraplength= 600
)
content.grid(column = 0, row = 1, sticky = tk.NSEW)
def page2(self):
title = tk.Label(
self.page_container,
background = self.color1,
foreground = self.color3,
height = 2,
font = ("Comic Sans", 20),
text = "Enter Outside Temperature "
)
title.grid(column = 0, row = 0)
#Allows the user to input an integer for the temperature
entry_Label = tk.Entry(self, width = 10)
entry_Label.grid(row = 0, column = 0)
#Defines the function that allows user to reset the entry
#def clearFunc():
#entry.delete(0,"end")
#Reset button allows user to delete inputted integers
#reset_button = tk.Button(text = "Reset", command = clearFunc)
#reset_button.grid(row = 0, column = 3)
#Convert buttons allows the user to convert temperatures from Celsius to Fahrenheit and vice versa
convert_button = tk.Button(self, text = "Convert")
convert_button.grid(row = 2, column = 0)
#Result label output
result_label = tk.Label(self, text ="")
result_label.grid(row = 2, column = 1)
content = tk.Label(
self.page_container,
background = self.color2,
foreground = self.color3,
justify = tk.LEFT,
anchor = tk.N,
pady = 20,
font = ("Comic Sans", 10),
wraplength= 600
)
content.grid(column = 0, row = 1, sticky = tk.NSEW)
root = tk.Tk()
root.title("Dave's Weather Recommendation app")
root.geometry('775x520')
root.resizable(width = False, height = False)
my_app_instance = Myapp(root)
root.mainloop()
Reesecup is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.