Notepad using tkinter in python
We use tkinter to make software. These software are Graphical user interface (GUI). It gives user a easier way for interaction. ‘tkinter’ module can be imported in python. Tk() and mainloop() are the methods which is used to create the main window and run the application until it is not closed. Tkinter provides various widgets like button, label, menu and canvas which can be used in the GUI application. There are three geometry manager which organises widgets in the main window. These are done through pack(), grid(), and place() method. pack() method organises the widgets into blocks. ‘grid()’ method organises the widget into the grid and place() method organises the widget in a specific position.
Function of the button widget is to provide a button into the application. Button can be used to perform different actions on the application. Button can be linked to a function which will be called when it is clicked. We can give active background, active foreground , border width and padding and several other features with the help of options which are passed as parameters.
Following program shows the button widget. In the following program options used are text, width, height, bg, activebackground and command.
import tkinter as tk
mainwindow = tk.Tk()
mainwindow.title('Main Window')
button = tk.Button(mainwindow, text='CLOSE', width='30', height='2',bg='blue',
activebackground='light blue',command=mainwindow.destroy)
button.pack()
mainwindow.mainloop()
Output:
Label widget displays texts and widgets. Entry widget is used for a single line string input from a user. The following program shows the use of entry widget. ‘grid() method’ is used to place the widgets grid like structure.
from tkinter import *
mainwindow = Tk()
Label(mainwindow, text='First object').grid(row=0)
Label(mainwindow, text='Second object').grid(row=1)
Label(mainwindow, text='Third object').grid(row=2)
Label(mainwindow, text='Fourth object').grid(row=3)
Label(mainwindow, text='Fifth object').grid(row=4)
entrywidget1 = Entry(mainwindow)
entrywidget1.grid(row=0, column=1)
entrywidget2 = Entry(mainwindow)
entrywidget2.grid(row=1, column=1)
entrywidget3 = Entry(mainwindow)
entrywidget3.grid(row=2, column=1)
entrywidget4 = Entry(mainwindow)
entrywidget4.grid(row=3, column=1)
entrywidget5 = Entry(mainwindow)
entrywidget5.grid(row=4, column=1)
mainloop()
Output:
Menu widget is used to create menus in the application. Following program shows the use of menu widget. It is used to create ‘File’, ‘Edit’, ‘Format’ and ‘Help’ menu. A number of options separated by comma can be used to format the widget. Here a command option is used in the file menu to exit or quit the application.
from tkinter import *
mainwindow = Tk()
thismenu = Menu(mainwindow)
mainwindow.config(menu=thismenu)
file = Menu(thismenu)
thismenu.add_cascade(label='File', menu=file)
file.add_command(label='New')
file.add_command(label='Open')
file.add_separator()
file.add_command(label='Save')
file.add_command(label='Save As')
file.add_separator()
file.add_command(label='Print')
file.add_command(label='Exit', command=mainwindow.quit)
Edit = Menu(thismenu)
thismenu.add_cascade(label='Edit')
Format = Menu(thismenu)
thismenu.add_cascade(label='Format')
help = Menu(thismenu)
thismenu.add_cascade(label='Help', menu=help)
help.add_command(label='About')
help.add_command(label='View Help')
mainloop()
Output:
Frame widget is used to organise the widget into a frame. It can be used for the grouping of the widgets. In the following program frame widget is used to organise or group the buttons in a frame.
from tkinter import *
mainwindow = Tk()
frame = Frame(mainwindow)
frame.pack()
redbutton = Button(frame, text='BUTTON 1', fg='red')
redbutton.pack(side=LEFT)
greenbutton = Button(frame, text='BUTTON 2', fg='green')
greenbutton.pack(side=RIGHT)
bluebutton = Button(frame, text='BUTTON 3', fg='blue')
bluebutton.pack(side=TOP)
blackbutton = Button(frame, text='BUTTON 4', fg='black')
blackbutton.pack(side=BOTTOM)
mainwindow.mainloop()
output:
Radio buttons are a multi choice buttons. A user can select any choice by the use of a radio button. Following program shows radio button with multiple choices where a user can select any one of the choices. Here every radio button is linked to the same variable and each radio button has its one value.
from tkinter import *
mainwindow = Tk()
choice = IntVar()
Radiobutton(mainwindow, text='CHOICE 1', variable=choice, value=1).pack(anchor=W)
Radiobutton(mainwindow, text='CHOICE 2', variable=choice, value=2).pack(anchor=W)
Radiobutton(mainwindow, text='CHOICE 3', variable=choice, value=3).pack(anchor=W)
Radiobutton(mainwindow, text='CHOICE 4', variable=choice, value=4).pack(anchor=W)
Radiobutton(mainwindow, text='CHOICE 5', variable=choice, value=5).pack(anchor=W)
Radiobutton(mainwindow, text='CHOICE 6', variable=choice, value=6).pack(anchor=W)
mainwindow.mainloop()
output :
Check buttons are used to check single or multiple choices. The following program shows the use of check buttons. Here a user can check single or multiple select choices.
from tkinter import *
mainwindow = Tk()
choice1 = IntVar()
Checkbutton(mainwindow, text='SELECT 1', variable=choice1).grid(row=0)
choice2 = IntVar()
Checkbutton(mainwindow, text='SELECT 2', variable=choice2).grid(row=1)
choice3 = IntVar()
Checkbutton(mainwindow, text='SELECT 3', variable=choice3).grid(row=2)
choice4 = IntVar()
Checkbutton(mainwindow, text='SELECT 4', variable=choice4).grid(row=3)
choice5 = IntVar()
Checkbutton(mainwindow, text='SELECT 5', variable=choice5).grid(row=4)
mainwindow.mainloop()
Output :
Scrollbar is used to give scrolling control to the users. Scrollbar can be of vertical as well as horizontal type. The following program shows the use of a scroll bar in a list box. Scrollbar is vertical scrollbar and is placed in the right side of the list box.
from tkinter import *
mainwindow = Tk()
scrollbar = Scrollbar(mainwindow)
scrollbar.pack(side=RIGHT, fill=Y)
list = Listbox(mainwindow, yscrollcommand=scrollbar.set)
for number in range(1, 20):
list.insert(END, 'Number ' + str(number))
list.pack(side=LEFT, fill=BOTH)
scrollbar.config(command=list.yview)
mainwindow.mainloop()
Output:
A simple text editor like Notepad can be made using tkinter. It is a GUI, means ‘Graphical User Interface’ having menus and sub menus. The following program shows a simple GUI notepad having menus- file, edit and help. It has also sub menus of new, open, save, cut, copy and paste.
from tkinter import *
import tkinter.messagebox as tmsg
from tkinter.filedialog import askopenfilename, asksaveasfilename
import os
def about():
tmsg.showinfo("About", "This is my notepad")
def cut():
Textwindow.focus_get().event_generate("<<Cut>>")
def copy():
Textwindow.focus_get().event_generate("<<Copy>>")
def paste():
Textwindow.focus_get().event_generate("<<Paste>>")
def new():
global file
mainwindow.title("My Notepad")
file = None
Textwindow.focus_get().delete(1.0, END)
def open():
global file
file = askopenfilename(defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file == "":
file = None
else:
mainwindow.title(os.path.basename(file) + " - My Notepad")
Textwindow.delete(1.0, END)
f = open(file, "r")
Textwindow.insert(1.0, f.read())
f.close()
def save():
global file
if file == None:
file = asksaveasfilename(initialfile = 'Untitled.txt',
defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if file =="":
file = None
else:
f = open(file, "w")
f.write(Textwindow.get(1.0, END))
f.close()
mainwindow.title(os.path.basename(file) + " - My Notepad")
else:
f = open(file, "w")
f.write(Textwindow.get(1.0, END))
f.close()
mainwindow = Tk()
mainwindow.geometry("650x450")
mainwindow.title("My Notepad")
Textwindow=Text(mainwindow)
Textwindow.pack(expand="True", fill=BOTH)
Addscroll=Scrollbar(Textwindow)
Addscroll.pack(side=RIGHT, fill=Y)
Addscroll.config(command=Textwindow.yview)
Textwindow=Text(mainwindow, yscrollcommand=Addscroll.set)
thismenu = Menu(mainwindow)
mainwindow.config(menu=thismenu)
file = Menu(thismenu)
thismenu.add_cascade(label='File', menu=file)
file.add_command(label='New', command=new)
file.add_command(label='Open', command=open)
file.add_command(label='Save', command=save)
file.add_separator()
file.add_command(label='Exit', command=mainwindow.quit)
Edit = Menu(thismenu)
thismenu.add_cascade(label="Edit", menu=Edit)
Edit.add_command(label='Cut', command=cut)
Edit.add_command(label='Copy', command=copy)
Edit.add_command(label='Paste', command=paste)
help = Menu(thismenu)
thismenu.add_cascade(label="Help", menu=help)
help.add_command(label='About', command=about)
mainwindow.mainloop()