GUI Programming using Tkinter Python

Hello all,

This is the eleventh article in the series Python for Data Science. If you are new to this series, we would recommend you to read our previous articles

  1. Python for Data Science Series - Part 1
  2. Python for Data Science Series - Part 2
  3. Using Numpy in Python
  4. Using Pandas in Python
  5. Data Visualization using Matplotlib Python
  6. Parsing XML file in python
  7. Parsing Json file in python
  8. Interactive Data Visualization using Plotly in Python
  9. Logging in Python
  10. Read and Write operations on Images using OpenCV Python


Tkinter is Python library which can be used to make GUI Application in Python. This is available in python by default.

In this article, we would explain the various functionalities available in Tkinter and also create a sample GUI Application which would look like below



Please refer the videos below for detailed explanation on how to use Tkinter to create GUI elements in Python


We have taken article from following webpage as reference

Please refer the following code to understand on how to create GUI elements using Tkinter in Python






In [1]:
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from tkinter import filedialog

window = Tk()
#window = tkinter.Tk()


window.title("Open Knowledge Share")

window.geometry('350x200')

lbl = Label(window, text="Enter Your Name")

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

txt = Entry(window,width=10)

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



combo = Combobox(window)

combo['values']= ('Select Values',1, 2, 3, 4, 5, "Text")

combo.current(0) #set the selected item

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


chk_state = BooleanVar()

chk_state.set(True) #set check state

chk = Checkbutton(window, text='Choose', var=chk_state)

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

radio_label = Label(window, text="Choose One of these")

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

selected = IntVar()

def clicked():

    #result = "Status: Welcome to " + txt.get()
    result = "Status: Selected " + combo.get()+"\n"
    result = result + "Choose is {} \n".format(chk_state.get())
    result = result + "selected Radio button   is {}".format(selected.get())
    status_text.configure(text= result)




rad1 = Radiobutton(window,text='First', value=1,command=clicked,variable=selected)

rad2 = Radiobutton(window,text='Second', value=2,command=clicked,variable=selected)

rad3 = Radiobutton(window,text='Third', value=3,command=clicked,variable=selected)

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

rad2.grid(column=1, row=6)

rad3.grid(column=2, row=6)


status_text = Label(window, text="Status : Idle")

status_text.grid(column=0, row=11)

def selectFile():
    #file = filedialog.askopenfilename()
    selected_path = filedialog.askopenfilename(filetypes = (("Text files","*.txt"),("CSV files","*.csv"),("all files","*.*")))
    #print(selected_path)
    #result = status_text.text
    result = ""
    result = result + "selected Path is {}".format(selected_path)
    status_text.configure(text= result)


def selectFolder():
    #file = filedialog.askopenfilename()
    selected_path = filedialog.askdirectory()
    #print(selected_path)
    #result = status_text.text
    result = ""
    result = result + "selected Path is {}".format(selected_path)
    status_text.configure(text= result)

file_label = Label(window, text="Choose your file")

file_label.grid(column=0, row=7)

file_btn = Button(window, text="Browse", command=selectFile)

file_btn.grid(column=2, row=7)



folder_label = Label(window, text="Choose your Folder")

folder_label.grid(column=0, row=8)

folder_btn = Button(window, text="Browse", command=selectFolder)

folder_btn.grid(column=2, row=8)




def create_popup():
    messagebox.showinfo('Popup window','Thanks for viewing Open Knowledge Share')

def closewindow():
    window.destroy()

btn = Button(window, text="Click Me", command=clicked)

btn.grid(column=2, row=12)

help_button = Button(window, text="Help", command=create_popup)

help_button.grid(column=1, row=12)

close_button = Button(window, text="Close", command=closewindow)

close_button.grid(column=3, row=12)



window.mainloop()
In [ ]:
 

Comments

Popular posts from this blog

How to run Jupyter Notebooks in Cloud

How to download , install and configure Anaconda - Python

Project: Implementation of Label Editor