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

Drawing Application

from tkinter import *

from tkinter.ttk import Scale

from tkinter import colorchooser,filedialog,messagebox

import PIL.ImageGrab as ImageGrab

class Draw():

    def __init__(self,root):

        self.root =root

        self.root.title("Painter")

        self.root.configure(background="white")

        self.pointer= "black"

        self.erase="white"    

        text=Text(root)

        text.tag_configure("tag_name", justify='center', font=('arial',25),background='#292826',foreground='orange')

        text.insert("1.0", "Drawing Application ")

        text.tag_add("tag_name", "1.0", "end")

        text.pack()

        self.pick_color = LabelFrame(self.root,text='Colors',font =('arial',15),bd=5,relief=RIDGE,bg="white")

        self.pick_color.place(x=0,y=40,width=90,height=185)

        colors = ['blue','red','green', 'orange','violet','black','yellow','purple','pink','gold','brown','indigo']

        i=j=0

        for color in colors:

            Button(self.pick_color,bg=color,bd=2,relief=RIDGE,width=3,command=lambda col=color:self.select_color(col)).grid(row=i,column=j)

            i+=1

            if i==6:

                i=0

                j=1

        self.eraser_btn= Button(self.root,text="Eraser",bd=4,bg='white',command=self.eraser,width=9,relief=RIDGE)

        self.eraser_btn.place(x=0,y=197)

        self.clear_screen= Button(self.root,text="Clear Screen",bd=4,bg='white',command= lambda : self.background.delete('all'),width=9,relief=RIDGE)

        self.clear_screen.place(x=0,y=227)

        self.save_btn= Button(self.root,text="ScreenShot",bd=4,bg='white',command=self.save_drawing,width=9,relief=RIDGE)

        self.save_btn.place(x=0,y=257)

        self.bg_btn= Button(self.root,text="Background",bd=4,bg='white',command=self.canvas_color,width=9,relief=RIDGE)

        self.bg_btn.place(x=0,y=287)

        self.pointer_frame= LabelFrame(self.root,text='size',bd=5,bg='white',font=('arial',15,'bold'),relief=RIDGE)

        self.pointer_frame.place(x=0,y=320,height=200,width=70)

        self.pointer_size =Scale(self.pointer_frame,orient=VERTICAL,from_ =48 , to =0, length=168)

        self.pointer_size.set(1)

        self.pointer_size.grid(row=0,column=1,padx=15)

        self.background = Canvas(self.root,bg='white',bd=5,relief=GROOVE,height=470,width=680)

        self.background.place(x=80,y=40)

        self.background.bind("<B1-Motion>",self.paint) 

    def paint(self,event):       

        x1,y1 = (event.x-2), (event.y-2)  

        x2,y2 = (event.x+2), (event.y+2)  

        self.background.create_oval(x1,y1,x2,y2,fill=self.pointer,outline=self.pointer,width=self.pointer_size.get())

    def select_color(self,col):

        self.pointer = col

    def eraser(self):

        self.pointer= self.erase

    def canvas_color(self):

        color=colorchooser.askcolor()

        self.background.configure(background=color[1])

        self.erase= color[1]

    def save_drawing(self):

        try:

            file_ss =filedialog.asksaveasfilename(defaultextension='jpg')

            x=self.root.winfo_rootx() + self.background.winfo_x()

            y=self.root.winfo_rooty() + self.background.winfo_y()

            x1= x + self.background.winfo_width() 

            y1= y + self.background.winfo_height()

            ImageGrab.grab().crop((x , y, x1, y1)).save(file_ss)

            messagebox.showinfo('Screenshot Successfully Saved as' + str(file_ss))

        except:

            print("Error in saving the screenshot")

if __name__ =="__main__":

    root = Tk()

    p= Draw(root)

    root.mainloop()

File Explorer

from tkinter import *

from tkinter import filedialog

def browseFiles():

    filename = filedialog.askopenfilename(initialdir = "/",

                                        title = "Select a File",

                                        filetypes = (("Text files",

                                                        "*.txt*"),

                                                    ("all files",

                                                        "*.*")))

    label_file_explorer.configure(text="File Opened: "+filename)                                                                                              

window = Tk()

window.title('File Explorer')

window.geometry("500x500")

window.config(background = "white")

label_file_explorer = Label(window,

                            text = "File Explorer using Tkinter",

                            width = 100, height = 4,

                            fg = "blue")

button_explore = Button(window,

                        text = "Browse Files",

                        command = browseFiles)

button_exit = Button(window,

                    text = "Exit",

                    command = exit)

label_file_explorer.grid(column = 1, row = 1)

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

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

window.mainloop()


Percentage Finder

from tkinter import *

def getPercentage() :

    students= int(total_participantField.get())

    rank = int(rankField.get())

    result = round((students - rank) / students * 100,3);

    percentageField.insert(10, str(result))

def Clear():

    rankField.delete(0, END)

    total_participantField.delete(0, END)

    percentageField.delete(0, END)

if __name__ == "__main__" :

    gui = Tk()

    gui.configure(background = "light pink")

    gui.title("Rank Based- percentage Calculator")

    gui.geometry("650x200")

    rank = Label(gui, text = "Rank", bg = "light green")

    andl = Label(gui, text = "And", bg = "light green")

    total_participant = Label(gui,

                            text = "Total Participants",

                            bg = "light green")

    find = Button(gui, text = "Find percentage",

                fg = "Black", bg = "Red",

                command = getPercentage)

    percentage = Label(gui, text = "percentage", bg = "light green")

    clear = Button(gui, text = "Clear",

                fg = "Black", bg = "Red",

                command = Clear)

    rank.grid(row = 1, column = 1,padx = 10)

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

    total_participant.grid(row = 1, column = 6, padx = 10)

    find.grid(row = 3, column = 4,pady = 10)

    percentage.grid(row = 4, column = 3,padx = 10)

    clear.grid(row = 5, column = 4,pady = 10)

    rankField = Entry(gui)

    total_participantField = Entry(gui)

    percentageField = Entry(gui)

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

    total_participantField.grid(row = 1, column = 7)

    percentageField.grid(row = 4, column = 4)

    

    gui.mainloop()


weight converter

from tkinter import *

window = Tk()

def from_kg():

gram = float(e2_value.get())*1000

pound = float(e2_value.get())*2.20462

ounce = float(e2_value.get())*35.274

t1.delete("1.0", END)

t1.insert(END,gram)

t2.delete("1.0", END)

t2.insert(END,pound)

t3.delete("1.0", END)

t3.insert(END,ounce)

e1 = Label(window, text = "Enter the weight in Kg")

e2_value = StringVar()

e2 = Entry(window, textvariable = e2_value)

e3 = Label(window, text = 'Gram')

e4 = Label(window, text = 'Pounds')

e5 = Label(window, text = 'Ounce')

t1 = Text(window, height = 1, width = 20)

t2 = Text(window, height = 1, width = 20)

t3 = Text(window, height = 1, width = 20)

b1 = Button(window, text = "Convert", command = from_kg)

e1.grid(row = 0, column = 0)

e2.grid(row = 0, column = 1)

e3.grid(row = 1, column = 0)

e4.grid(row = 1, column = 1)

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

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

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

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

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

window.mainloop()


Compound Interest GUI Calculator using Tkinter

# importing classes and functions from the tkinter

from tkinter import *

# Function for clearing the

# contents of all entry boxes

def clear_all() :

# whole content of entry boxes is deleted

principle_field.delete(0, END)

rate_field.delete(0, END)

time_field.delete(0, END)

compound_field.delete(0, END)

# set focus on the principle_field entry box

principle_field.focus_set()

# Function to find compound interest

def calculate_ci():

# get a content from entry box

principle = int(principle_field.get())

rate = float(rate_field.get())

time = int(time_field.get())

# Calculates compound interest

CI = principle * (pow((1 + rate / 100), time))

# insert method inserting the

# value in the text entry box.

compound_field.insert(10, CI)

#code

if __name__ == "__main__" :

# Create a GUI window

root = Tk()

# Set the background colour of GUI window

root.configure(background = 'white')

# Set the configuration of GUI window

root.geometry("400x250")

# set the name of tkinter GUI window

root.title("Compound Interest Calculator")

# Create a Principle Amount : label

label1 = Label(root, text = "Principle Amount(Rs) : ",

fg = 'white', bg = 'black')

# Create a Rate : label

label2 = Label(root, text = "Rate(%) : ",

fg = 'white', bg = 'black')

# Create a Time : label

label3 = Label(root, text = "Time(years) : ",

fg = 'white', bg = 'black')


# Create a Compound Interest : label

label4 = Label(root, text = "Compound Interest : ",

fg = 'white', bg = 'black')

# grid method is used for placing

# the widgets at respective positions

# in table like structure .

# padx keyword argument used to set padding along x-axis .

# pady keyword argument used to set padding along y-axis .

label1.grid(row = 1, column = 0, padx = 10, pady = 10)

label2.grid(row = 2, column = 0, padx = 10, pady = 10)

label3.grid(row = 3, column = 0, padx = 10, pady = 10)

label4.grid(row = 5, column = 0, padx = 10, pady = 10)

# Create a entry box

# for filling or typing the information.

principle_field = Entry(root)

rate_field = Entry(root)

time_field = Entry(root)

compound_field = Entry(root)

# grid method is used for placing

# the widgets at respective positions

# in table like structure .

# padx keyword argument used to set padding along x-axis .

# pady keyword argument used to set padding along y-axis .

principle_field.grid(row = 1, column = 1, padx = 10, pady = 10)

rate_field.grid(row = 2, column = 1, padx = 10, pady = 10)

time_field.grid(row = 3, column = 1, padx = 10, pady = 10)

compound_field.grid(row = 5, column = 1, padx = 10, pady = 10)

# Create a Submit Button and attached

# to calculate_ci function

button1 = Button(root, text = "Submit", bg = "red",

fg = "black", command = calculate_ci)

# Create a Clear Button and attached

# to clear_all function

button2 = Button(root, text = "Clear", bg = "red",

fg = "black", command = clear_all)

button1.grid(row = 4, column = 1, pady = 10)

button2.grid(row = 6, column = 1, pady = 10)

# Start the GUI

root.mainloop()


Tkinter-Text Widget

from tkinter import *

root = Tk()

root.geometry("300x300")

root.title(" Q&A ")

def Take_input():

INPUT = inputtxt.get("1.0", "end-1c")

print(INPUT)

if(INPUT == "120"):

Output.insert(END, 'Correct')

else:

Output.insert(END, "Wrong answer")

l = Label(text = "What is 24 * 5 ? ")

inputtxt = Text(root, height = 10,

width = 25,

bg = "light yellow")

Output = Text(root, height = 5,

width = 25,

bg = "light cyan")

Display = Button(root, height = 2,

width = 20,

text ="Show",

command = lambda:Take_input())

l.pack()

inputtxt.pack()

Display.pack()

Output.pack()

mainloop()


Python Calendar GUI

from tkinter import *

import calendar

def showCalender():

    gui = Tk()

    gui.config(background='grey')

    gui.title("Calender for the year")

    gui.geometry("550x600")

    year = int(year_field.get())

    gui_content= calendar.calendar(year)

    calYear = Label(gui, text= gui_content, font= "Consolas 10 bold")

    calYear.grid(row=5, column=1,padx=20)

    gui.mainloop()

if __name__=='__main__':

    new = Tk()

    new.config(background='grey')

    new.title("Calender")

    new.geometry("250x140")

    cal = Label(new, text="Calender",bg='grey',font=("times", 28, "bold"))

    year = Label(new, text="Enter year", bg='dark grey')

    year_field=Entry(new)

    button = Button(new, text='Show Calender',

fg='Black',bg='Blue',command=showCalender)

    cal.grid(row=1, column=1)

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

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

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

    new.mainloop()

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()