Hi,
I am new here. I came here because I need help with GUI for my python project. I don't understand how to make GUI (using tkinter), so please can u help me?
My project is just code witch converts time in formate like 6.82 (you have to use . instead of ,) hour converts into 6 hours and 49 minutes. Can you help me with just simple GUI pls? I want to learn it.
def enterNumber():
global FloatHours
FloatHours = input('FloatHours = ')
try:
float(FloatHours)
except ValueError:
print('Invalid number. Try again.')
enterNumber()
enterNumber()
hours = int(float(FloatHours))
minutes = int(((float(FloatHours) * 60) % 60))
HoursAndMinutes = f'{hours} hours and {minutes} minutes.'
print(HoursAndMinutes)
end = input('End')
There are many (oh so many!) tutorials online for writing tkinter programs. You'll do better stepping through one or two of those than trying to work through the forum. The forum is a better tool when you have a specific question.
Hello Qwertz, here is your required gui code. I am also beginner to tkinter and gui creation but with the help of google this is what I made. Hope it works fine for you.
# importing the required libraries
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
# creating an instance of tkinter window
win = Tk()
# set geometry of your frame
win.geometry('550x300')
# your function to get values from user in tkinter widget
def enterNumber():
try:
hours = int(float(entry.get()))
minutes = int(((float(entry.get()) * 60) % 60))
except:
messagebox.showerror('Error', 'Not a number.')
Label(win, text = ('%s hours and %s minutes.')%(hours, minutes), font=('Century 16')).pack(pady = 20)
# create an Entry widget
entry = ttk.Entry(win, font = ('Century 12'), width = 40)
entry.pack(pady = 30)
# create a button to display the output
button = ttk.Button(win, text = 'Enter', command = enterNumber)
button.pack()
win.mainloop()
# enjou with peace
[
attachment=1540]
to update the label on new entry
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
# creating an instance of tkinter window
win = Tk()
mytext = StringVar()
mytext.set("")
# set geometry of your frame
win.geometry('550x300')
# your function to get values from user in tkinter widget
def enterNumber():
try:
minutes = int(float(entry.get()))
hours = int(((minutes) / 60) % 60)
mytext.set(f'{hours} hours and {(int(minutes) - int((hours * 60)))} minutes')
except:
messagebox.showerror('Error', 'Not a number.')
# create an Entry widget
entry = ttk.Entry(win, font = ('Century 12'), width = 40)
entry.pack(pady = 30)
# create a button to display the output
button = ttk.Button(win, text = 'Enter', command = enterNumber)
button.pack()
Label(win, textvariable = mytext, font=('Century 16')).pack(pady = 20)
win.mainloop()
I did a version that updates the label as well.
import tkinter as tk
from tkinter import messagebox
root = tk.Tk()
root['padx'] = 8
def error_message(arg):
messagebox.showerror('Errror!', f'{arg} is not permitted')
my_var = tk.StringVar()
nvar = tk.StringVar()
nvar.set('Hours: 0 Minutes: 0')
def _time(var, index, mode):
try:
hrs = int(float(my_var.get()))
minutes = int(float(my_var.get())*60%60)
nvar.set(f'Hours: {hrs} Minutes: {minutes}')
except ValueError:
error_message(my_var.get())
my_var.set('')
my_var.trace_add('write', _time)
entry = tk.Entry(root, width=50, textvariable=my_var)
entry.grid(column=0, row=0, pady=5)
entry.focus()
label = tk.Label(root, textvariable=nvar)
label.grid(column=0, row=1, pady=5, sticky='w')
root.mainloop()