Showing posts with label Tkinter. Show all posts
Showing posts with label Tkinter. Show all posts

Simple GUI calculator using Tkinter

from tkinter import *

expression = ""

def press(num):

global expression

expression = expression + str(num)

equation.set(expression)

def equalpress():

try:

global expression

total = str(eval(expression))

equation.set(total)

expression = ""

except:

equation.set(" error ")

expression = ""

def clear():

global expression

expression = ""

equation.set("")

if __name__ == "__main__":

gui = Tk()

gui.configure(background="light green")

gui.title("Simple Calculator")

gui.geometry("270x150")

equation = StringVar()

expression_field = Entry(gui, textvariable=equation)

expression_field.grid(columnspan=4, ipadx=70)

button1 = Button(gui, text=' 1 ', fg='black', bg='red',

command=lambda: press(1), height=1, width=7)

button1.grid(row=2, column=0)

button2 = Button(gui, text=' 2 ', fg='black', bg='red',

command=lambda: press(2), height=1, width=7)

button2.grid(row=2, column=1)

button3 = Button(gui, text=' 3 ', fg='black', bg='red',

command=lambda: press(3), height=1, width=7)

button3.grid(row=2, column=2)

button4 = Button(gui, text=' 4 ', fg='black', bg='red',

command=lambda: press(4), height=1, width=7)

button4.grid(row=3, column=0)

button5 = Button(gui, text=' 5 ', fg='black', bg='red',

command=lambda: press(5), height=1, width=7)

button5.grid(row=3, column=1)

button6 = Button(gui, text=' 6 ', fg='black', bg='red',

command=lambda: press(6), height=1, width=7)

button6.grid(row=3, column=2)

button7 = Button(gui, text=' 7 ', fg='black', bg='red',

command=lambda: press(7), height=1, width=7)

button7.grid(row=4, column=0)

button8 = Button(gui, text=' 8 ', fg='black', bg='red',

command=lambda: press(8), height=1, width=7)

button8.grid(row=4, column=1)

button9 = Button(gui, text=' 9 ', fg='black', bg='red',

command=lambda: press(9), height=1, width=7)

button9.grid(row=4, column=2)

button0 = Button(gui, text=' 0 ', fg='black', bg='red',

command=lambda: press(0), height=1, width=7)

button0.grid(row=5, column=0)

plus = Button(gui, text=' + ', fg='black', bg='red',

command=lambda: press("+"), height=1, width=7)

plus.grid(row=2, column=3)

minus = Button(gui, text=' - ', fg='black', bg='red',

command=lambda: press("-"), height=1, width=7)

minus.grid(row=3, column=3)

multiply = Button(gui, text=' * ', fg='black', bg='red',

command=lambda: press("*"), height=1, width=7)

multiply.grid(row=4, column=3)

divide = Button(gui, text=' / ', fg='black', bg='red',

command=lambda: press("/"), height=1, width=7)

divide.grid(row=5, column=3)

equal = Button(gui, text=' = ', fg='black', bg='red',

command=equalpress, height=1, width=7)

equal.grid(row=5, column=2)

clear = Button(gui, text='Clear', fg='black', bg='red',

command=clear, height=1, width=7)

clear.grid(row=5, column='1')

Decimal= Button(gui, text='.', fg='black', bg='red',

command=lambda: press('.'), height=1, width=7)

Decimal.grid(row=6, column=0)

gui.mainloop()


Color game using Tkinter

import tkinter

import random

colours = ['Red','Blue','Green','Pink','Black',

'Yellow','Orange','White','Purple','Brown']

score = 0

timeleft = 30

def startGame(event):

if timeleft == 30:

countdown()

nextColour()

def nextColour():

global score

global timeleft

if timeleft > 0:

e.focus_set()

if e.get().lower() == colours[1].lower():

score += 1

e.delete(0, tkinter.END)

random.shuffle(colours)

label.config(fg = str(colours[1]), text = str(colours[0]))

scoreLabel.config(text = "Score: " + str(score))

def countdown():

global timeleft

if timeleft > 0:

timeleft -= 1

timeLabel.config(text = "Time left: "

+ str(timeleft))

timeLabel.after(1000, countdown)

root = tkinter.Tk()

root.title("COLORGAME")

root.geometry("375x200")

instructions = tkinter.Label(root, text = "Type in the colour"

"of the words, and not the word text!",

font = ('Helvetica', 12))

instructions.pack()

scoreLabel = tkinter.Label(root, text = "Press enter to start",

font = ('Helvetica', 12))

scoreLabel.pack()


timeLabel = tkinter.Label(root, text = "Time left: " +

str(timeleft), font = ('Helvetica', 12))

timeLabel.pack()


label = tkinter.Label(root, font = ('Helvetica', 60))

label.pack()

e = tkinter.Entry(root)

root.bind('<Return>', startGame)

e.pack()

e.focus_set()

root.mainloop()


Create a stopwatch using python

import tkinter as Tkinter

from datetime import datetime

counter = 66600

running = False

def counter_label(label):

def count():

if running:

global counter

if counter==66600:

display="Starting..."

else:

tt = datetime.fromtimestamp(counter)

string = tt.strftime("%H:%M:%S")

display=string


label['text']=display 

label.after(1000, count)

counter += 1

count()

def Start(label):

global running

running=True

counter_label(label)

start['state']='disabled'

stop['state']='normal'

reset['state']='normal'

def Stop():

global running

start['state']='normal'

stop['state']='disabled'

reset['state']='normal'

running = False

def Reset(label):

global counter

counter=66600

if running==False:

reset['state']='disabled'

label['text']='Welcome!'

else:

label['text']='Starting...'

root = Tkinter.Tk()

root.title("Stopwatch")

root.minsize(width=250, height=70)

label = Tkinter.Label(root, text="Welcome!", fg="black", font="Verdana 30 bold")

label.pack()

f = Tkinter.Frame(root)

start = Tkinter.Button(f, text='Start', width=6, command=lambda:Start(label))

stop = Tkinter.Button(f, text='Stop',width=6,state='disabled', command=Stop)

reset = Tkinter.Button(f, text='Reset',width=6, state='disabled', command=lambda:Reset(label))

f.pack(anchor = 'center',pady=5)

start.pack(side="left")

stop.pack(side ="left")

reset.pack(side="left")

root.mainloop()


Counter using Tkinter GUI in Python

 import tkinter as tk

counter = 0
def counter_label(label):
def count():
global counter
counter += 1
label.config(text=str(counter))
label.after(1000, count)
count()

root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
counter_label(label)
button = tk.Button(root, text='Stop', width=25, command=root.destroy)
button.pack()
root.mainloop()