This is a Python project that implements a simple GUI application using the tkinter library to convert distances in miles to kilometres.
Prerequisites Python 3.x tkinter library ttkthemes library
Installation Make sure you have Python 3.x installed on your system. Install the required libraries by running the following command: pip install tkinter ttkthemes
Usage
-
Import the necessary libraries: import tkinter as tk from tkinter import ttk from ttkthemes import ThemedTk
-
Define the conversion function: def convert(): mile_input = entry_int.get() km_output = mile_input * 1.61 output_string.set(str(km_output))
-
Center the window on the screen: def center_window(window, width, height): screen_width = window.winfo_screenwidth() screen_height = window.winfo_screenheight() x = (screen_width/2) - (width/2) y = (screen_height/2) - (height/2) window.geometry(f"{width}x{height}+{int(x)}+{int(y)}")
-
Create the main window: window = ThemedTk(theme="arc") window.title('Miles to Kilometers Converter') center_window(window, 400, 150)
-
Design the user interface: 5.1. Add a title label: title_label = ttk.Label(master=window, text='Miles to Kilometers', font='Calibri 18 bold') title_label.pack(pady=10)
5.2. Create an input field: input_frame = ttk.Frame(master=window) entry_int = tk.IntVar() miles_label = ttk.Label(master=input_frame, text="Miles:") entry = ttk.Entry(master=input_frame, textvariable=entry_int, width=10) button = ttk.Button(master=input_frame, text='Convert', command=convert) miles_label.pack(side='left') entry.pack(side='left', padx=(0, 10)) button.pack(side='left') input_frame.pack(pady=(0, 10))
5.3. Add an output label: output_string = tk.StringVar() output_label = ttk.Label( master=window, text='Output:', font='Calibri 18', textvariable=output_string) output_label.pack(pady=5)
- Run the application: window.mainloop()
License This project is licensed under the MIT License.
Acknowledgements This project utilizes the tkinter library for GUI development. The ttkthemes library is used to apply a theme to the application's window.