Friday, 9 August 2024

Python - Tkinter (GUI)

 Tkinter:  (Toolkit Interface)

--------

    -Tkinter is Python’s standard GUI (Graphical User Interface) package. 

    -Tkinter Toolkit used to create GUI Applications(Desktop Applications)


Tkinter Types

----------------

1. Tk Dialogs(Messagebox dialogs,question dialogs, File dialogs)

2. Tk widgets(widgets like labels, textbox,buttons, frames, menus etc. to the window)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1. Tkinder Dialogs

  ----------------

  - The messagebox module is used to display the message boxes in the python applications.


  A. messagebox dialogs

     ------------------

    1). showinfo()

    2). showwarning()

    3). showerror()


      Declaration In Python:

      ----------------------  

        import tkinter

        from tkinter import messagebox


  B. Question dialogs

      --------------

    1). askokcancel()

    2). askyesno()

    3). askretrycancel()

      

      Declaration In Python:

      ----------------------  

import tkinter

        from tkinter import messagebox


  C. file dialogs

     -----------

    1).askopenfilename

    2).asksaveasfilename

    3).askdirectory


      Declaration In Python:

      ----------------------  

from tkinter import *

from tkinter import filedialog

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 1: Display message box in Python

import tkinter

from tkinter import messagebox


#message box display

messagebox.showerror("Error", "Error message")

messagebox.showwarning("Warning","Warning message")

messagebox.showinfo("Information","Informative message")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 2: Display Question Dialog in Python

import tkinter

from tkinter import messagebox


#question dialog display

messagebox.askokcancel("Title","The application will be closed")

messagebox.askyesno("Title","Do you want to save?")

messagebox.askretrycancel("Title","Installation failed, try again?")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 3 : File dialogs

from tkinter import *

from tkinter import filedialog


filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))

filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))

filedialog.askdirectory()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

=======================================================

2. Tk Widgets

 ------------

-Tk widgets(widgets like labels, textbox,buttons, frames, menus etc. to the window)


1. Button

2. Label

3. Entry(textbox)

4. Dropdown

5. RadioButton

6. CheckButton

7. Menubar

8. Listbox

9. Scrollbar

10. Canvas

11. Frame

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 1 : create a window

from tkinter import *

window = Tk()

window.mainloop()

#window.withdraw()

~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 2 : create a window(Title,size & Location)

from tkinter import *

window = Tk()


window.title('Jahab')

window.minsize(500,300) #width,height

#window.geometry('600x400+200+50') #width,height,x,y

#window.resizable(False, False)

#window.attributes('-alpha', 0.5)  

#window.iconbitmap('1.jpg')

window.mainloop()

-------------------------

Python Tkinter Geometry:

-----------------------

-The Tkinter geometry specifies the method by using which, the widgets are represented on display. 

-The python Tkinter provides the following geometry methods.


1) pack() - widget in the block

2) grid() - widget in the tabular form

3) place() - widget to the specific x and y coordinates

-----------------------------

#program 3: Display button

from tkinter import *

window = Tk()

window.title('Button')

window.geometry('100x100')


b1=Button(window,pady=10,padx=10,text="Red",bg="red",fg="yellow",activeforeground = "white",activebackground = "black")  

b1.pack()

window.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~

Note:

 padx - x axis

 pady - y axis

 fg - Foreground colour

 bg - background colour 

~~~~~~~~~~~~~~~~~~~~~~~~~

#program 4 : using button with click event

from tkinter import *

from tkinter import messagebox


def msg():

    messagebox.showinfo("Hai","Welcome")


window = Tk()

window.title('using fucntion')

window.minsize(500,300) 

b1 = Button(window, text="OK", command=msg)

b1.pack()

window.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#program 5: Login Form - Label,Textbox & Button (using Pack)

from tkinter import *

window = Tk()

window.title('Login Form')

window.geometry('200x100')


name=Label(window,text="Name",bg='red',fg='yellow')

t1 = Entry(window)


password=Label(window,text="Password",bg='red',fg='yellow')

t2=Entry(window,show="*")


submit=Button(window,text="Submit")


name.pack()

t1.pack()

password.pack()

t2.pack()

submit.pack()

window.mainloop()  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#program 6: Login Form - Label,Textbox & Button (using Grid)

from tkinter import *

window = Tk()

window.title('Login Form')

window.geometry('200x100')


name=Label(window,text="Name",bg='red',fg='yellow').grid(row=0,column=0,pady=10,padx=10)

t1 = Entry(window).grid(row=0,column=1,pady=10,padx=10)


password=Label(window,text="Password",bg='red',fg='yellow').grid(row=1,column=0)  

t2=Entry(window,show="*").grid(row=1,column=1)


submit=Button(window,text="Submit").grid(row=4,column=0,columnspan=2,pady=10,padx=10)  

window.mainloop()  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#program 7: Login Form - Label,Textbox & Button (using place)

from tkinter import *

window = Tk()

window.title('Login Form')

window.geometry('250x150')


name=Label(window,text="Name",bg='red',fg='yellow').place(x=5,y=50)

t1 = Entry(window).place(x=55,y=50)


password=Label(window,text="Password",bg='red',fg='yellow').place(x=5,y=75)

t2=Entry(window,show="*").place(x=70,y=75)


submit=Button(window,text="Submit").place(x=30,y=100)

window.mainloop()  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#program 8: Display RadioButton

from tkinter import *

window = Tk()


gender=Label(window,text="Gender",bg='red',fg='yellow').grid(row=0,column=0,pady=10,padx=10)

r1=Radiobutton(window,text="male",value=1).grid(row=0,column=1,pady=10,padx=10)

r2=Radiobutton(window,text="female",value=2).grid(row=0,column=2,pady=10,padx=10)

window.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#program 9: display CheckButton

from tkinter import *

window = Tk()


hobbies=Label(window,text="Hobbies",bg='red',fg='yellow').grid(row=0,column=0,pady=10,padx=10)

c1=Checkbutton(window,text="sports").grid(row=0,column=1,pady=10,padx=10)

c2=Checkbutton(window,text="Music").grid(row=0,column=2,pady=10,padx=10)

c3=Checkbutton(window,text="TV").grid(row=0,column=3,pady=10,padx=10)

window.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 10 : Display Dropdown

from tkinter import *

window = Tk()


variable = StringVar(window)

variable.set("Select City")

city=Label(window,text="city",bg='red',fg='yellow').grid(row=0,column=0,pady=10,padx=10)

d1 = OptionMenu(window,variable,"one", "two", "three").grid(row=0,column=1,pady=10,padx=10)

~~~~~~~~~~~~~~~~~~~~~~~~

#Program 11 : Display Listbox & Scrollbar

from tkinter import *

window = Tk()


scrollbar=Scrollbar(window) 

scrollbar.pack(side= RIGHT,fill = Y ) 

   

a1=Listbox(window,yscrollcommand=scrollbar.set)

for item in ["Select City","Chennai","Salem","Trichy","coimbatore","Madurai","Apple","orange","mango","Banana","tirhy"]:

  a1.insert(END,item)

a1.pack()

window.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#program 12: using Menubar

from tkinter import *

master = Tk()


def donothing():

   x = 0


menubar=Menu(master)


filemenu = Menu(menubar, tearoff=0)

filemenu.add_command(label="New", command=donothing)

filemenu.add_command(label="Open", command=donothing)

filemenu.add_command(label="Save", command=donothing)

filemenu.add_separator()

filemenu.add_command(label="Exit", command=master.quit)

menubar.add_cascade(label="File", menu=filemenu)

 

helpmenu = Menu(menubar, tearoff=0)

helpmenu.add_command(label="Help Index", command=donothing)

helpmenu.add_command(label="About...", command=donothing)

menubar.add_cascade(label="Help", menu=helpmenu)

 

master.config(menu=menubar)

master.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--------------------------------------

Python Graphics using Canvas

-------------------------------------

-The Canvas widget provides structured graphics facilities for Tkinter

-It can be used to create charts, custom widgets, or create games.

-It can be used to create Lines, Colours, Shapes, Drawing Image & Drawing Text


The Canvas widget supports the following standard items:

---------------------------------------------------------------------

1) text

2) line

3) rectangle

4) oval (a circle or an ellipse)

5) arc

6) polygon

7) image

8) bitmap

9) window

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 1 : Draw a string using canvas

from tkinter import *

app=Tk()

#app Title

app.title("Display String")


#canvas

canvas = Canvas(app,width=300, height=300, bg='yellow')  

canvas.pack(expand=YES, fill=BOTH)


#Draw string(x,y,string)

canvas.create_text(100, 180, text='Jahab')

canvas.create_text(100, 150, text='Jahab',font=('arial',25),fill='blue')


#calling Main

app.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 2 : Draw a Line using canvas

from tkinter import *

app=Tk()

#app Title

app.title("Line")


#canvas

canvas = Canvas(app,width=300, height=300, bg='yellow')  

canvas.pack(expand=YES, fill=BOTH)


#Draw Line(x1,y1,x2,y2)

canvas.create_line(10, 10, 200, 200)

canvas.create_line(20, 50, 100, 100,width=10, fill='green')

canvas.create_line(10, 10, 200, 200,dash=(8, 6))  #4px dash, 2px space


#calling Main

app.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 3 : Draw a Rectangle using canvas

from tkinter import *

app=Tk()

#app Title

app.title("Rectangle")


#canvas

canvas = Canvas(app,width=300, height=300, bg='yellow')  

canvas.pack(expand=YES, fill=BOTH)


#Draw Rectangle(x1,y1,width,height)

canvas.create_rectangle(200, 200, 300, 300)

canvas.create_rectangle(100, 100, 200, 200, width=5, fill='red')

canvas.create_rectangle(100, 100, 200, 200, width=5, fill='red',outline='white')


#calling Main

app.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 4 : Draw a Oval using canvas

from tkinter import *

app=Tk()

#app Title

app.title("Oval")


#canvas

canvas = Canvas(app,width=300, height=300, bg='yellow')  

canvas.pack(expand=YES, fill=BOTH)


#Draw oval(x1,y1,width,height)

canvas.create_oval(10, 10, 200, 200)

canvas.create_oval(20, 20, 200, 200, width=2, fill='blue')


#calling Main

app.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 5 : Draw a arc using canvas

from tkinter import *

app=Tk()

#app Title

app.title("Arc")


#canvas

canvas = Canvas(app,width=300, height=300, bg='yellow')  

canvas.pack(expand=YES, fill=BOTH)


#Draw arc

points=10,10,50,50 #coordinates

canvas.create_arc(points,start=0,extent=270,fill='green')

points1=50,100,150,150 #coordinates

canvas.create_arc(points1,start=180,extent=270)


#calling Main

app.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 6 : Draw a polygon using canvas

from tkinter import *

app=Tk()

#app Title

app.title("Polygon")


#canvas

canvas = Canvas(app,width=300, height=300, bg='yellow')  

canvas.pack(expand=YES, fill=BOTH)


#draw polygon

points = [150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200]

canvas.create_polygon(points, outline='white',fill='red', width=2)


#calling Main

app.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 7 : Display Images

from tkinter import *

app=Tk()

#app Title

app.title("Display Image")


#canvas

canvas = Canvas(app,width=300, height=300, bg='yellow')  

canvas.pack(expand=YES, fill=BOTH)


#create image

MyImage=PhotoImage(file='e:\\1.gif')

#MyImage=PhotoImage(file='e:\\1.png')

#MyImage=PhotoImage(file='e:\\2.jpg') #error

canvas.create_image(0,0,anchor=NW,image=MyImage)


#calling Main

app.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#Program 8 : create a bitmap(from existing)

from tkinter import *

app=Tk()

#app Title

app.title("Display Image")


#canvas

canvas = Canvas(app,width=300, height=300, bg='yellow')  

canvas.pack(expand=YES, fill=BOTH)


canvas.create_bitmap(50, 50, bitmap='questhead')

canvas.create_bitmap(50, 70, bitmap='error')

canvas.create_bitmap(50, 90, bitmap='hourglass')

canvas.create_bitmap(50, 110, bitmap='info')

canvas.create_bitmap(50, 130, bitmap='question')

canvas.create_bitmap(50, 150, bitmap='warning')


#calling Main

app.mainloop()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

No comments:

Post a Comment