Python Forum
[Tkinter] How to add conversions and fix button positions.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to add conversions and fix button positions.
#1
Hi everybody, I have recently started learning to make Python Gui's using the Tkinter model. However, I need help with doing a conversion like this.
https://youtu.be/1z41yet2DkI

Here is what I have so far though it's not complete and the conversions are still missing and I have the combobox with the list of meters and feet.

from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Length Converter")

mainframe = ttk.Frame(root, padding= "25 25 25 25")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

meters = StringVar()
feet = StringVar()
n = StringVar()

meters_entry = ttk.Entry(mainframe, width=7, textvariable=meters)
meters_entry.grid(column=2, row=1, sticky=(W, E))

choices = ttk.Combobox (root, width = 10, textvariable = n)
choices ['values'] = ('meters', 'feet')
choices.grid(column = 1, row = 1)


ttk.Label(mainframe, text="is equal to").grid(column=1, row=2, sticky=E)
ttk.Button(mainframe, text="Calculate").grid(column=2, row=3, sticky=W)

root.mainloop()
Reply
#2
How do you expect your program to work? If I wanted to convert meters to feet, what would be the steps I need to take?
Reply
#3
Well I expect the program to work when the user types 25 and chooses either meters or feet and when the user selects one of those it shows the conversion. So if I chose 25 and then select meters, it would convert to feet. If I chose feet it would convert to meters.
Reply
#4
That logic limits you to converting between two units. You couldn't add millimeters or furlongs or miles. For units conversion usually there are two lists of units; the units for the input and the units for the output.
Reply
#5
I'm talking about how do you add the calculation depending on what the user selects from the dropdown box from meters or feet. I want to convert meters to feet when I select meters and then feet to meters according to that video I was assigned. It has to function like in the video.
Reply
#6
Oh, homework, not real work. Got it.

How do you think it should work? If feet is selected how would you convert the number from meters to feet? Can you write a function to do that? Can you write another function that converts feet to meters?

Once you have the code for each function, how do you decide what function to call? Since "choices" is used to select the conversion you will have to get the current selection from choices and execute one of the conversion functions based on the selection. Do you know how to get the selection? Do you know how to write code that will do one thing or another based on some comparison?
Reply
#7
Yes, would I use something like a defined function along with if, when the dropdown choice is selected to meters it would convert to feet, and if the dropdown choice is set to feet it would convert to meters. Then after that, I can set the message at the bottom just like how it is in the video but I don't know where to start or how.
Reply
#8
Write the conversion functions first. Next you could write the program to only convert from meters to feet, ignoring the selection in the choices combobox. Once you get that working, change the program so it only converts from feet to meters. Verify both conversions work.

After that I would change the program to just print out the combo box selection. Select feet, press the calculate button and see what it prints. Select meters, press the button and see what it prints.

Now you have all the parts. You know how to convert from feet to meters, and how to convert from meters to feet. You know how to get the value of the combo box. All you have to do is add an if statement to call the conversion function based on the combo box value.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Programmatically creating buttons that remember their positions Clunk_Head 6 1,331 Jun-22-2023, 02:51 PM
Last Post: deanhystad
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,998 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  GUI Tkinter Widget Positions punksnotdead 3 2,955 Jun-12-2019, 06:06 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020