Raspberry-Pi Programming Efforts

in Raspberry Pi3 years ago

December 8th, 2020

Efforts to Learn


I have been slowly trying to learn to program using my Raspberry-Pi. I've learned a little bit about guizero, I can make a "Wanted Poster" and almost actually understand how I did it and why it worked. (see Last post)

Through it I learned how to change colors and fonts and font size and how to import pictures.

Now I am trying to learn how to make something functional. One of the Links that @mytechtrail provided me was to Real Python Tutorial Python GUI Programming With Tkinter. Contained in that was a temperature conversion example:

real python temp coversion program

I played around with it quite a bit the last couple of days and this is my incomplete version with a few modifications that I learned:

my temp coversion so far.png

Real Python version is a fixed size window, that when you type a number in the Celsius symbol will shift and a label will hold the conversion when you click the arrow button.

One of the first things I modified was making the window re- sizable along the horizontal, window.resizable(width=True, height=False) (which in the finished version I will likely go back to false for resize option). I also played with the background colors as practice. It did take me some research to find out how to change the main window background color.

Where bg="color" worked for the button and the label, it did not work for setting the window color, however I did find .configure(bg="salmon") command that let me change the color. I still do not understand why bg="color" would not work so a comment if you can explain would be appreciated. Is it a global seting verse a local setting thing?

Now about the Reset button. I want that to clear the input box and to give it the focus when the button is pushed.

btn_clear = tk.Button(text="Reset")
#ent_temperature.entry.delete(0, tk.end)

That is what I have for now, it does not clear the input box.

Below is my modified code, followed by the Real python code example.



import tkinter as tk

#gui.configure(bg='blue')


def fahrenheit_to_celsius():
    """Convert the value for Fahrenheit to Celsius and insert the
    result into lbl_result.
    """
    fahrenheit = ent_temperature.get()
    celsius = (5/9) * (float(fahrenheit) - 32)
    lbl_result["text"] = f"{round(celsius, 2)} \N{DEGREE CELSIUS}"

# Set-up the window
window = tk.Tk()
window.title("Temperature Converter")
window.resizable(width=True, height=False)
window.configure(bg="salmon")
# Create the Fahrenheit entry frame with an Entry
# widget and label in it
frm_entry = tk.Frame(master=window)
ent_temperature = tk.Entry(master=frm_entry, width=5)
lbl_temp = tk.Label(master=frm_entry, text="\N{DEGREE FAHRENHEIT}")

# Layout the temperature Entry and Label in frm_entry
# using the .grid() geometry manager
ent_temperature.grid(row=0, column=0, sticky="e")
lbl_temp.grid(row=0, column=1, sticky="w")

# Create the conversion Button and result display Label
btn_convert = tk.Button(
    master=window,
    text="\N{RIGHTWARDS BLACK ARROW}",
    command=fahrenheit_to_celsius,
    bg="yellow"
)
lbl_result = tk.Label(master=window, text=" \N{DEGREE CELSIUS}", bg="lightblue",
                      width=7)

btn_clear = tk.Button(text="Reset")
#ent_temperature.entry.delete(0, tk.end)

# Set-up the layout using the .grid() geometry manager
frm_entry.grid(row=0, column=0, padx=10)
btn_convert.grid(row=0, column=1, pady=10)
lbl_result.grid(row=0, column=2, padx=15)
btn_clear.grid(row=0, column=3, padx=10)

# Run the application
window.mainloop()

Real python Code:

import tkinter as tk

def fahrenheit_to_celsius():
    """Convert the value for Fahrenheit to Celsius and insert the
    result into lbl_result.
    """
    fahrenheit = ent_temperature.get()
    celsius = (5/9) * (float(fahrenheit) - 32)
    lbl_result["text"] = f"{round(celsius, 2)} \N{DEGREE CELSIUS}"

# Set-up the window
window = tk.Tk()
window.title("Temperature Converter")
window.resizable(width=False, height=False)

# Create the Fahrenheit entry frame with an Entry
# widget and label in it
frm_entry = tk.Frame(master=window)
ent_temperature = tk.Entry(master=frm_entry, width=10)
lbl_temp = tk.Label(master=frm_entry, text="\N{DEGREE FAHRENHEIT}")

# Layout the temperature Entry and Label in frm_entry
# using the .grid() geometry manager
ent_temperature.grid(row=0, column=0, sticky="e")
lbl_temp.grid(row=0, column=1, sticky="w")

# Create the conversion Button and result display Label
btn_convert = tk.Button(
    master=window,
    text="\N{RIGHTWARDS BLACK ARROW}",
    command=fahrenheit_to_celsius
)
lbl_result = tk.Label(master=window, text="\N{DEGREE CELSIUS}")

# Set-up the layout using the .grid() geometry manager
frm_entry.grid(row=0, column=0, padx=10)
btn_convert.grid(row=0, column=1, pady=10)
lbl_result.grid(row=0, column=2, padx=10)

# Run the application
window.mainloop()

Any help, (or guidance where to find the answer), figuring out the Reset button I want would be appreciated. It is a slow process, but I do feel like I am making some headway.

Tiny Picture links back to my blog:

Sort:  

pixresteemer_incognito_angel_mini.png
Bang, I did it again... I just rehived your post!
Week 35 of my contest just started...you can now check the winners of the previous week!
!BEER
7

Looks like you're having some fun! What ultimately are you planning to do with your Raspberry Pi?

I plan on it being my major on line computer in the future, for now I am using it to learn how to do some programming.

You need to stake more BEER (24 staked BEER allows you to call BEER one time per day)

It seems complicated. Good you are making headway with the program.

It is a simple typo can be hard to find, and finding help is sometimes not easy for a real beginner like myself.

I'm impressed. I find it very hard for my non trained brain to learn such things, but the more I play around with it, the more I understand and retain. Looks like your plugging away and learning stuff. Good luck with figuring it all out and having fun in the meantime.

I do find it fun to try and focus on figuring something out but I can get lost for hours which isn't a bad thing being retired and all, but then I end up with less time on other things. But it does keep the brain functioning better than Amazon videos.