Python Forum
GUI for simple python project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI for simple python project
#1
Question 
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')
Reply
#2
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.
Qwertz likes this post
Reply
#3
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
   
Reply
#4
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()
Hilal and BashBedlam like this post
Reply
#5
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()
Hilal likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Forum Jump:

User Panel Messages

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