Sample 1
from tkinter import *
class BuckyButtons:
def __init__(self,master):
self.Label_0 = Label (root, text = "Welcome to the Bank Asia App\n ").pack()
self.Label_1= Label(root,text="Current Balance:").pack()
Current_balance=5000 # we are assuing that this account has $5000 already
self.account_balance = Current_balance
self.Label_2 = Label(root, text = "") # showing the second line the amount of money
self.Label_2.pack()
self.Label_3= Label(root,text="") # showing the messages after the execution of each processes
self.Label_3.pack()
self.eEntry = Entry(root)
self.eEntry.pack() #this is the entry box where we are going to input the amount of money
frame = Frame(root) # gui front page created
frame.pack()
topFrame = Frame(root) # the front frame is devide into two parts one is top frame and other is bottom frame
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack()
#BUTTONS
self.Deposit_Money = Button(topFrame, text= "Deposit",command= self.deposit) #Deposit button
self.Deposit_Money.pack(side=LEFT)
self.Withdraw_Money = Button(topFrame, text= "Withdraw Money", command= self.withdrawl) #withdrawl button
self.Withdraw_Money.pack(side=LEFT)
self.Check_Balance = Button(frame, text= "Check Balance", command = self.Check_Balance) #check balance button
self.Check_Balance.pack(side=BOTTOM)
self.quitButton = Button(bottomFrame, text="Quit", command=frame.quit) #quit button
self.quitButton.pack( side=TOP )
def deposit(self):
self.x= float(self.eEntry.get()) # gets only the numberic inputs from text box
self.account_balance += self.x
self.Label_2.config(text= self.account_balance) #changes the balance
self.show_message= ("You have successfully deposited $" , format(self.eEntry.get()))
self.Label_3.config(text= self.show_message) #shows message about the money deposited
def Check_Balance(self):
self.Label_2.config(text= self.account_balance)
self.show_message = "Your account balance is " , format(self.account_balance)
self.Label_3.config(text=self.show_message)
def withdrawl(self):
z= float(self.eEntry.get())
if z<= self.account_balance: #the condition "if" detemines if money to be withdrawl or not, this is the true part
self.account_balance-=z
self.Label_2.config(text=self.account_balance)
self.show_message= ("You have successfully Withdraw $" , format(self.eEntry.get()))
self.Label_3.config(text= self.show_message)
if z>self.account_balance: #this is the false part
self.Label_2.config(text="Error! not enough balance")
root = Tk()
root.geometry('450x450') #determines the the frame pop up
root.title('Bank Operation') #name of the gui
b = BuckyButtons(root)
root.mainloop()
sample 2
from tkinter import*
class Paint_app(object):
DEFAULT_PEN_SIZE = 6.0 # Seting the default values
#default_colour = "black"
def __init__(self):
self.root = Tk()
self.root.title("Paint application") #tile of the Gui
self.v= StringVar() #this is to accept the string varible from the entry box
#menu created
menu1 = Menu(self.root)
self.root.config(menu = menu1 )
submenu = Menu(menu1)
menu1.add_cascade(label ="file", menu = submenu) #name of the menu
submenu.add_command(label="New", command = self.New_file) #submenus under the menu , this submenu 'new ' will generate a new file again
submenu.add_command(label="Save") #apparently i tried a lot but couldnot make it work
submenu.add_command(label="Close", command = quit) #quits the whole program
self.frame= Frame(self.root) # Gui window being created
self.frame.pack()
#Buttons
self.pen_button = Button(self.frame, text='pen', command = self.use_pen ) #pen button
self.pen_button.grid(row=0, column=0)
self.color_button = Button(self.frame, text='colour',command=self.change_colour) #color button to change the color
self.color_button.grid(row=0, column=1)
self.eEntry = Entry(self.frame,textvariable= self.v) #takes user input about which colors they want
self.eEntry.grid(row=0,column = 2)
self.Label_1 = Label(self.frame, text = "Type color names\n Eg: red, blue etc") #indicates the user what to do in the entry box
self.Label_1.grid(row = 1 , column = 2)
self.eraser_button = Button(self.frame, text='eraser',command=self.use_eraser) #Erase buttoms
self.eraser_button.grid(row=0, column=3)
self.choose_size_button = Scale(self.frame, from_=1, to=30, orient=HORIZONTAL) # allows to change the pen size
self.choose_size_button.grid(row=0, column=4)
self.Label_2 = Label(self.frame, text = "Pen size") #indicate where to change the pen size
self.Label_2.grid(row = 1 , column = 4)
self.c= Canvas(self.frame, bg = 'white', width= 1200 , height = 600) #Canvas is created
self.c.grid(row=2 , columnspan = 5) #asigning the number of rows and columns
self.setup() # calling the function under which the operations in canvas are defined
self.root.mainloop() #just runs everything you can see at the top
def New_file(self): #this is the submenu which will generate a new file
self.c= Canvas(self.frame, bg = 'white', width= 1200 , height = 1200) #
self.c.grid(row=1 , columnspan = 5)
self.setup()
self.root.mainloop()
"""def save_file():
filename = "my_drawings.jpg"
self.draw.save(filename)"""
def setup(self):
self.old_x = None #position of x axis is nothing unless your mouse is clicked and is on motion
self.old_y = None #psotion of y axis is also nothing unless your mouse is clicked and is on motion
self.line_width = self.choose_size_button.get()
self.color = 'black' #default background is black
self.eraser_on = False #starting of the program eraser will not be in use
self.active_button = self.pen_button # pen will be activated from the staring
self.c.bind('<B1-Motion>', self.paint) #this will work when the mouse is in motion
self.c.bind('<ButtonRelease-1>', self.reset) #stop drawing
def change_colour(self): #allows to take the input from the text box and change the color of the font
x=self.v.get()
self.color = x
def use_pen(self):
self.activate_button(self.pen_button) #pen buttom goes down
def use_eraser(self): #eraser buttom goes down
self.activate_button(self.eraser_button, eraser_mode=True)
def activate_button(self, some_button, eraser_mode=False): # allows the button to rise up and down
self.active_button.config(relief=RAISED) #buttom rise
some_button.config(relief=SUNKEN) #buttom down
self.active_button = some_button
self.eraser_on = eraser_mode
def paint(self, event):
self.line_width = self.choose_size_button.get()
paint_color = 'white' if self.eraser_on else self.color # if eraser buttton is on the paint color will change to white
if self.old_x and self.old_y: #painting while the mouse is clicked and is in motion
self.c.create_line(self.old_x, self.old_y, event.x, event.y,
width=self.line_width, fill=paint_color,
capstyle=ROUND, smooth=TRUE, splinesteps=36)
self.old_x = event.x #where ever the cursor stops its the present x and y which is event
self.old_y = event.y
def reset(self, event):
self.old_x, self.old_y = None, None
if __name__ == '__main__': #running the individual file only
ge = Paint_app()