└── Tkinter ├── widget_button.py ├── widget_label.py ├── widget_label_Design.py ├── widget_button_Design.py ├── Digital_Clock.py ├── Todo_List.py ├── on_Click_button.py ├── close screen.py ├── frame.py ├── close_Buttons.py ├── widget_textbox.py ├── IntroTkinter.py ├── geometry.py ├── grid.py ├── label_textbox_button_widget.py ├── widget_Checkbox.py ├── Snake_Game.py ├── calculator.py └── Notepad.py /Tkinter/widget_button.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | 3 | window = tk.Tk() 4 | window.title('Button Widget') 5 | 6 | # Add button widget 7 | button = tk.Button(text="Click Me") 8 | button.pack() 9 | # closing widget 10 | window.mainloop() 11 | -------------------------------------------------------------------------------- /Tkinter/widget_label.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | 3 | window = tk.Tk() 4 | window.title('Label Widget') 5 | 6 | # Add label widget 7 | label = tk.Label(text="Python rocks!") 8 | label.pack() 9 | label1 = tk.Label(text="We are learning PYTHON-101") 10 | label1.pack() 11 | # closing widget 12 | window.mainloop() 13 | -------------------------------------------------------------------------------- /Tkinter/widget_label_Design.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | 3 | window = tk.Tk() 4 | window.title('Label Widget') 5 | 6 | 7 | label = tk.Label( 8 | text="We are learning PYTHON-101", 9 | foreground="white", # Set the text color to white 10 | background="gray", # Set the background color to black 11 | width=50, 12 | height=50 13 | ) 14 | label.pack() 15 | # closing widget 16 | window.mainloop() 17 | -------------------------------------------------------------------------------- /Tkinter/widget_button_Design.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | 3 | window = tk.Tk() 4 | window.title('Button Widget') 5 | 6 | # opening widget 7 | button = tk.Button( 8 | text="Click Me!", 9 | foreground="white", # Set the text color to white 10 | background="gray", # Set the background color to black 11 | width=25, 12 | height=5 13 | ) 14 | button.pack() 15 | # closing widget 16 | window.mainloop() 17 | -------------------------------------------------------------------------------- /Tkinter/Digital_Clock.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import time 3 | 4 | root = Tk() 5 | root.geometry("400x100") 6 | root.config(bg='black') 7 | 8 | 9 | def update(): 10 | clock.config(text=time.strftime("%I:%M:%S")) 11 | clock.after(1000, update) 12 | 13 | 14 | clock = Label(root, background='black', foreground='white', font=('arial', 40, 'bold')) 15 | clock.pack() 16 | update() 17 | root.title('Digital clock Using Tkinter') 18 | root.mainloop() 19 | -------------------------------------------------------------------------------- /Tkinter/Todo_List.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | window = Tk() 3 | 4 | label= Label(window,text = "TO-DO-LIST") 5 | label.pack() 6 | 7 | list = Listbox(window,bg = 'yellow') 8 | list.insert(1,'Clean your stuff') 9 | list.insert(2,'Create a Tkinter program') 10 | list.insert(3,'Finish application') 11 | list.insert(4,'Get a Refill') 12 | list.pack() 13 | 14 | window.config(bg='black') 15 | window.geometry("300x300") 16 | window.title('LIST') 17 | window.mainloop() -------------------------------------------------------------------------------- /Tkinter/on_Click_button.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | root.title("Display GUI") 5 | root.geometry("400x400") 6 | 7 | 8 | def clickme(): 9 | hello = "Hello " + nameLabel.get() 10 | Label(root, text=hello).pack(pady=10) 11 | nameLabel.delete(0, 'end') 12 | 13 | 14 | nameLabel = Entry(root, width=10, font=('Helvetica', 30)) 15 | nameLabel.pack(padx=10, pady=10) 16 | 17 | Button(root, text="Enter Your Name", command=clickme).pack(pady=10) 18 | 19 | root.mainloop() 20 | -------------------------------------------------------------------------------- /Tkinter/close screen.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | 4 | # create close method that includes the root.destroy command 5 | def close(): 6 | root.destroy() 7 | 8 | 9 | # To create the tkinter window 10 | root = Tk() 11 | root.geometry('200x100') 12 | 13 | # create the button and pass the close method as the command. 14 | # the close method contains the root.destroy 15 | # we can directly pass the destroy command instead of creating a close method 16 | button = Button(root, text='Close the window', command=close) 17 | button.pack(pady=20) 18 | 19 | root.mainloop() 20 | -------------------------------------------------------------------------------- /Tkinter/frame.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | root.geometry("655x434") 6 | root.maxsize(1200, 989) 7 | root.minsize(200, 100) 8 | 9 | f1 = Frame(root, bg="gray", borderwidth=6, relief=SUNKEN) 10 | f1.pack(side=LEFT, fill=Y) 11 | 12 | f2 = Frame(root, bg="grey", borderwidth=8, relief=SUNKEN) 13 | f2.pack(side=TOP, fill=X) 14 | 15 | label_1 = Label(f1, text="Project", fg="green") 16 | label_1.pack(pady=14) 17 | 18 | label_2 = Label(f2, text="Welcome to PyCharm IDE", font="Helvetica 16 bold", fg="green", pady=22) 19 | label_2.pack() 20 | 21 | root.mainloop() 22 | -------------------------------------------------------------------------------- /Tkinter/close_Buttons.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | # tkinter window 4 | root = Tk() 5 | 6 | # This button can close the window 7 | button_1 = Button(root, text=" Close the Window", command=root.destroy) 8 | 9 | button_1.pack(pady=50) 10 | 11 | # This button closes the first button 12 | button_2 = Button(root, text=" Close the first button", command=button_1.destroy) 13 | button_2.pack(pady=50) 14 | 15 | # This button closes the second button 16 | button_3 = Button(root, text=" Close the second button", command=button_2.destroy) 17 | button_3.pack(pady=50) 18 | 19 | mainloop() 20 | -------------------------------------------------------------------------------- /Tkinter/widget_textbox.py: -------------------------------------------------------------------------------- 1 | # The interesting bit about Entry widgets isn’t how to style them, though. It’s how to use them to get input from a user. There are three main operations that you can perform with Entry widgets: 2 | # 3 | # Retrieving text with .get() 4 | # Deleting text with .delete() 5 | # Inserting text with .insert() 6 | import tkinter as tk 7 | 8 | window = tk.Tk() 9 | window.title('Text Box Widget') 10 | 11 | # Add button widget 12 | label = tk.Label(text="Enter Here") 13 | entry = tk.Entry() 14 | label.pack() 15 | entry.pack() 16 | # closing widget 17 | window.mainloop() 18 | 19 | -------------------------------------------------------------------------------- /Tkinter/IntroTkinter.py: -------------------------------------------------------------------------------- 1 | # Tkinter Programming 2 | # Tkinter is the standard GUI library for Python. 3 | # Python when combined with Tkinter provides a fast and easy way to create GUI applications. 4 | # Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit. 5 | 6 | # The foundational element of a Tkinter GUI is the window. 7 | # Windows are the containers in which all other GUI elements live. 8 | # These other GUI elements, such as text boxes, labels, and buttons, are known as widgets. 9 | # Widgets are contained inside of windows. 10 | 11 | 12 | import tkinter as tk 13 | window = tk.Tk() 14 | '''Any Widgets you wants are added here ''' 15 | window.mainloop() 16 | -------------------------------------------------------------------------------- /Tkinter/geometry.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | 5 | # geometry use to make window (width,height) 6 | root.geometry("655x344") 7 | root.maxsize(1200, 989) # Max size of window (width,height) 8 | root.minsize(200, 100) # Min size of window (width , height) 9 | 10 | root.title(" Geometry window") 11 | 12 | r = Label(text="This is my first tkinter program", bg="Gray", fg="white", padx=113, pady=94, font="comicsansms 19 bold", 13 | borderwidth=30, relief=RIDGE) 14 | 15 | # r.pack(fill= BOTH, expand=True) # fill = BOTH and expand = True use to fit in the screen. 16 | 17 | r.pack(side=BOTTOM, anchor="se") # anchor to move in (s = south ,e = east , n = north ,w = west) 18 | 19 | root.mainloop() 20 | -------------------------------------------------------------------------------- /Tkinter/grid.py: -------------------------------------------------------------------------------- 1 | # import tkinter module 2 | from tkinter import * 3 | 4 | # creating main tkinter window/toplevel 5 | master = Tk() 6 | 7 | # this will create a label widget 8 | l1 = Label(master, text = "First:") 9 | l2 = Label(master, text = "Second:") 10 | 11 | # grid method to arrange labels in respective 12 | # rows and columns as specified 13 | l1.grid(row = 0, column = 0, sticky = W, pady = 2) 14 | l2.grid(row = 1, column = 0, sticky = W, pady = 2) 15 | 16 | # entry widgets, used to take entry from user 17 | e1 = Entry(master) 18 | e2 = Entry(master) 19 | 20 | # this will arrange entry widgets 21 | e1.grid(row = 0, column = 1, pady = 2) 22 | e2.grid(row = 1, column = 1, pady = 2) 23 | 24 | # infinite loop which can be terminated by keyboard 25 | # or mouse interrupt 26 | mainloop() 27 | -------------------------------------------------------------------------------- /Tkinter/label_textbox_button_widget.py: -------------------------------------------------------------------------------- 1 | # Program to make a simple 2 | # login screen 3 | 4 | 5 | import tkinter as tk 6 | 7 | root = tk.Tk() 8 | root.title('Login Screen') 9 | 10 | # setting the windows size 11 | root.geometry("600x400") 12 | 13 | # declaring string variable 14 | # for storing name and password 15 | name_var = tk.StringVar() 16 | password_var = tk.StringVar() 17 | 18 | 19 | # defining a function that will 20 | # get the name and password and 21 | # print them on the screen 22 | def submit(): 23 | name = name_var.get() 24 | password = password_var.get() 25 | 26 | print("The name is : " + name) 27 | print("The password is : " + password) 28 | 29 | name_var.set("Enter Your Name") 30 | password_var.set("Enter Your Password") 31 | 32 | 33 | # creating a label for 34 | # name using widget Label 35 | name_label = tk.Label(root, text='Username', font=('calibre', 10, 'bold')) 36 | 37 | # creating an entry for input 38 | # name using widget Entry 39 | name_entry = tk.Entry(root, textvariable=name_var, font=('calibre', 10, 'normal')) 40 | 41 | # creating a label for password 42 | password_label = tk.Label(root, text='Password', font=('calibre', 10, 'bold')) 43 | 44 | # creating a entry for password 45 | password_entry = tk.Entry(root, textvariable=password_var, font=('calibre', 10, 'normal'), show='*') 46 | 47 | # creating a button using the widget 48 | # Button that will call the submit function 49 | sub_btn = tk.Button(root, text='Submit', command=submit) 50 | 51 | # placing the label and entry in 52 | # the required position using grid 53 | # method 54 | name_label.grid(row=0, column=0) 55 | name_entry.grid(row=0, column=1) 56 | password_label.grid(row=1, column=0) 57 | password_entry.grid(row=1, column=1) 58 | sub_btn.grid(row=2, column=1) 59 | 60 | # performing an infinite loop 61 | # for the window to display 62 | root.mainloop() 63 | -------------------------------------------------------------------------------- /Tkinter/widget_Checkbox.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | root.geometry("655x344") 5 | 6 | 7 | def get_value(): 8 | print("It works!\n") 9 | print("Name:-", nameValue.get(), "\nPhone:- ", phoneValue.get(), "\nGender:-", genderValue.get(), 10 | "\nEmergency Number:-", emergencyValue.get(), "\n Payment Mode:-", payment_modeValue.get(), 11 | "\n food Service :- ", food_serviceValue.get()) 12 | 13 | 14 | # Heading. 15 | label1 = Label(root, text="Creating Checkbox GUI", font="comicsansms 13 bold", pady=10, fg="black").grid(column=2) 16 | 17 | # Text for the form. 18 | name = Label(root, text="Name ") 19 | name.grid(row=1, column=1) 20 | 21 | phone = Label(root, text="Phone ") 22 | phone.grid(row=2, column=1) 23 | 24 | gender = Label(root, text="Gender ") 25 | gender.grid(row=3, column=1) 26 | 27 | emergency = Label(root, text="Emergency Number ") 28 | emergency.grid(row=4, column=1) 29 | 30 | payment_mode = Label(root, text="Payment Mode ") 31 | payment_mode.grid(row=5, column=1) 32 | 33 | # tkinter value to storing enties. 34 | nameValue = StringVar() 35 | phoneValue = StringVar() 36 | genderValue = StringVar() 37 | emergencyValue = StringVar() 38 | payment_modeValue = StringVar() 39 | food_serviceValue = IntVar() # IntVar because it would be 0 or 1. 40 | 41 | # Entries for our form 42 | 43 | nameEntry = Entry(root, textvariable=nameValue) 44 | nameEntry.grid(row=1, column=2) 45 | 46 | phoneEntry = Entry(root, textvariable=phoneValue) 47 | phoneEntry.grid(row=2, column=2) 48 | 49 | genderEntry = Entry(root, textvariable=genderValue) 50 | genderEntry.grid(row=3, column=2) 51 | 52 | emergencyEntry = Entry(root, textvariable=emergencyValue) 53 | emergencyEntry.grid(row=4, column=2) 54 | 55 | payment_modeEntry = Entry(root, textvariable=payment_modeValue) 56 | payment_modeEntry.grid(row=5, column=2) 57 | 58 | # making checkbox and packing 59 | food_service = Checkbutton(root, text="Check here", variable=food_serviceValue) 60 | food_service.grid(row=6, column=2) 61 | 62 | button1 = Button(root, text="Submit", command=get_value) 63 | button1.grid(row=7, column=2, pady=10) 64 | 65 | root.mainloop() 66 | -------------------------------------------------------------------------------- /Tkinter/Snake_Game.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | import random, time 3 | 4 | 5 | class Snake(Tk): 6 | def __init__(self, *arge, **kwargs): 7 | Tk.__init__(self, *arge, **kwargs) 8 | self.initialSetup() 9 | 10 | def initialSetup(self): 11 | self.base = Canvas(self, width=500, height=500) 12 | self.base.pack(padx=10, pady=10) 13 | self.snake = self.base.create_rectangle(1, 1, 21, 21, fill="DodgerBlue2") 14 | self.score = 0 15 | self.scoreDisplay = Label(self, text="Score:{}".format(self.score), font=('arial', 20, 'bold')) 16 | self.scoreDisplay.pack(anchor='n') 17 | self.length = 3 18 | self.target = None 19 | self.gameStatus = 1 20 | self.x = 20 21 | self.y = 0 22 | self.bodycoords = [(0, 0)] 23 | self.bind('', self.linkKeys) 24 | return 25 | 26 | def check_snake_coords(self): 27 | self.base.move(self.snake, self.x, self.y) 28 | i, j, ii, jj = self.base.coords(self.snake) 29 | if i <= 0 or j <= 0 or ii >= 500 or jj >= 500: 30 | self.x = 0 31 | self.y = 0 32 | # gameover 33 | self.base.create_text(220, 220, text="GAME OVER", font=('arial', 40, 'bold'), fill='red') 34 | self.gameStatus = 0 35 | return 36 | 37 | def move_snake(self): 38 | i, j, ii, jj = self.base.coords(self.snake) 39 | ii = (ii - ((ii - i) / 2)) 40 | jj = (jj - ((jj - j) / 2)) 41 | self.bodycoords.append((ii, jj)) 42 | self.base.delete('snakebody') 43 | if len(self.bodycoords) >= self.length: 44 | self.bodycoords = self.bodycoords[-self.length:] 45 | self.base.create_line(tuple(self.bodycoords), tag='snakebody', width=20, fill="DodgerBlue2") 46 | return 47 | 48 | def food(self): 49 | if self.target == None: 50 | a = random.randint(20, 480) 51 | b = random.randint(20, 480) 52 | self.target = self.base.create_oval(a, b, a + 20, b + 20, fill='red', tag='food') 53 | # print(self.base.coords(self.target)) 54 | if self.target: 55 | # print(self.base.coords(self.target)) 56 | i, j, ii, jj = self.base.coords(self.target) 57 | # time.sleep(0.1) 58 | if len(self.base.find_overlapping(i, j, ii, jj)) != 1: 59 | self.base.delete("food") 60 | self.target = None 61 | self.updateScore() 62 | self.length += 1 63 | return 64 | 65 | def updateScore(self): 66 | self.score += 1 67 | self.scoreDisplay['text'] = "Score : {}".format(self.score) 68 | return 69 | 70 | def linkKeys(self, event=None): 71 | pressedkey = event.keysym 72 | if pressedkey == 'Left': 73 | self.x = -20 74 | self.y = 0 75 | 76 | elif pressedkey == 'Up': 77 | self.x = 0 78 | self.y = -20 79 | 80 | elif pressedkey == 'Right': 81 | self.x = 20 82 | self.y = 0 83 | 84 | elif pressedkey == 'Down': 85 | self.x = 0 86 | self.y = 20 87 | 88 | else: 89 | pass 90 | return 91 | 92 | def manage(self): 93 | if (self.gameStatus == 0): 94 | return 95 | self.check_snake_coords() 96 | self.move_snake() 97 | self.food() 98 | 99 | return 100 | 101 | 102 | snakeobj = Snake(className="Snake Game") 103 | while True: 104 | snakeobj.update() 105 | snakeobj.update_idletasks() 106 | snakeobj.manage() 107 | time.sleep(0.4) 108 | -------------------------------------------------------------------------------- /Tkinter/calculator.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | 3 | root = Tk() 4 | root.title("Calculator") 5 | root.configure(background="black") 6 | 7 | root.geometry("340x555") 8 | root.minsize(340, 555) 9 | root.maxsize(340, 555) 10 | 11 | 12 | def click(event): 13 | global scvalue 14 | text = event.widget.cget("text") 15 | # print(text) 16 | if text == "=": 17 | if scvalue.get().isdigit(): 18 | value = int(scvalue.get()) 19 | else: 20 | value = eval(screen.get()) 21 | scvalue.set(value) 22 | screen.update() 23 | 24 | elif text == "Clear": 25 | scvalue.set("") 26 | screen.update() 27 | 28 | else: 29 | scvalue.set(scvalue.get() + text) 30 | screen.update() 31 | 32 | 33 | scvalue = StringVar() 34 | scvalue.set("") 35 | 36 | screen = Entry(root, textvar=scvalue, font="lucida 30 bold") 37 | screen.pack(fill=X, pady=10, padx=10) 38 | 39 | f = Frame(root, bg="black") 40 | 41 | b = Button(f, text="Clear", bg="gray", padx=15, pady=15, font="lucida 25 bold", width=12) 42 | b.pack(side=LEFT) 43 | b.bind("", click) 44 | 45 | f.pack() 46 | 47 | f = Frame(root, bg="black") 48 | 49 | b = Button(f, text="9", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 50 | b.pack(side=LEFT, padx=5, pady=5) 51 | b.bind("", click) 52 | 53 | b = Button(f, text="8", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 54 | b.pack(side=LEFT, padx=5, pady=5) 55 | b.bind("", click) 56 | 57 | b = Button(f, text="7", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 58 | b.pack(side=LEFT, padx=5, pady=5) 59 | b.bind("", click) 60 | 61 | b = Button(f, text="/", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 62 | b.pack(side=LEFT, padx=5, pady=5) 63 | b.bind("", click) 64 | 65 | f.pack() 66 | 67 | f = Frame(root, bg="black") 68 | 69 | b = Button(f, text="6", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 70 | b.pack(side=LEFT, padx=5, pady=5) 71 | b.bind("", click) 72 | 73 | b = Button(f, text="5", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 74 | b.pack(side=LEFT, padx=5, pady=5) 75 | b.bind("", click) 76 | 77 | b = Button(f, text="4", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 78 | b.pack(side=LEFT, padx=5, pady=5) 79 | b.bind("", click) 80 | 81 | b = Button(f, text="*", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 82 | b.pack(side=LEFT, padx=5, pady=5) 83 | b.bind("", click) 84 | 85 | f.pack() 86 | 87 | f = Frame(root, bg="black") 88 | 89 | b = Button(f, text="3", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 90 | b.pack(side=LEFT, padx=5, pady=5) 91 | b.bind("", click) 92 | 93 | b = Button(f, text="2", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 94 | b.pack(side=LEFT, padx=5, pady=5) 95 | b.bind("", click) 96 | 97 | b = Button(f, text="1", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 98 | b.pack(side=LEFT, padx=5, pady=5) 99 | b.bind("", click) 100 | 101 | b = Button(f, text="+", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 102 | b.pack(side=LEFT, padx=5, pady=5) 103 | b.bind("", click) 104 | 105 | f.pack() 106 | 107 | f = Frame(root, bg="black") 108 | 109 | b = Button(f, text=".", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 110 | b.pack(side=LEFT, padx=5, pady=5) 111 | b.bind("", click) 112 | 113 | b = Button(f, text="0", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 114 | b.pack(side=LEFT, padx=5, pady=5) 115 | b.bind("", click) 116 | 117 | b = Button(f, text="=", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 118 | b.pack(side=LEFT, padx=5, pady=5) 119 | b.bind("", click) 120 | 121 | b = Button(f, text="-", bg="gray", padx=10, pady=10, font="lucida 25 bold", width=2) 122 | b.pack(side=LEFT, padx=5, pady=5) 123 | b.bind("", click) 124 | 125 | f.pack() 126 | 127 | root.mainloop() 128 | -------------------------------------------------------------------------------- /Tkinter/Notepad.py: -------------------------------------------------------------------------------- 1 | # GUI Notepad 2 | 3 | from tkinter import * 4 | from tkinter.messagebox import showinfo 5 | from tkinter.filedialog import askopenfilename, asksaveasfilename 6 | import os 7 | 8 | 9 | def newFile(): 10 | global file 11 | root.title("Untitled - Notepad") 12 | file = None 13 | TextArea.delete(1.0, END) 14 | 15 | 16 | def openFile(): 17 | global file 18 | 19 | file = askopenfilename(defaultextension=".txt", filetypes=[("All files", "*.*"), ("Text Documents", "*.txt")]) 20 | 21 | if file == "": 22 | file = None 23 | else: 24 | root.title(os.path.basename(file) + "- Notepad") 25 | TextArea.delete(1.0, END) 26 | 27 | f = open(file, "r") 28 | TextArea.insert(1.0, f.read()) 29 | 30 | f.close() 31 | 32 | 33 | def saveFile(): 34 | global file 35 | if file == None: 36 | file = asksaveasfilename(initialfile="Untitled.txt", defaultextension=".txt", 37 | filetypes=[("All files", "*.*"), ("Text Documents", "*.txt")]) 38 | 39 | if file == "": 40 | file = None 41 | 42 | else: 43 | # Save it as a New File 44 | f = open(file, "w") 45 | f.write(TextArea.get(1.0, END)) 46 | f.close() 47 | 48 | root.title(os.path.basename(file) + " - Notepad") 49 | else: 50 | f = open(file, "w") 51 | f.write(TextArea.get(1.0, END)) 52 | f.close() 53 | 54 | 55 | def quitApp(): 56 | root.destroy() 57 | 58 | 59 | def cut(): 60 | TextArea.event_generate(("<>")) 61 | 62 | 63 | def copy(): 64 | TextArea.event_generate(("<>")) 65 | 66 | 67 | def paste(): 68 | TextArea.event_generate(("<>")) 69 | 70 | 71 | def about(): 72 | showinfo("About Notepad", "Notepad by Sajid Majeed") 73 | 74 | 75 | if __name__ == '__main__': 76 | # ---------------- Basic tkinter setup ---------------- 77 | # Creating an instance of tkinter 78 | root = Tk() 79 | # Adding title 80 | root.title("Notepad") 81 | # Setting icon 82 | # root.iconbitmap("icon.ico") 83 | # Setting default size 84 | root.geometry("644x588") 85 | # Setting minimum size 86 | root.minsize(600, 500) 87 | 88 | # ---------------- Creating a Menu Bar ---------------- 89 | MenuBar = Menu(root) 90 | root.config(menu=MenuBar) 91 | 92 | # File Menu 93 | FileMenu = Menu(MenuBar, tearoff=0) 94 | # To open a New File 95 | FileMenu.add_command(label="New", command=newFile) 96 | # To open already existing File 97 | FileMenu.add_command(label="Open", command=openFile) 98 | # To save the current file 99 | FileMenu.add_command(label="Save", command=saveFile) 100 | # To add a separating line 101 | FileMenu.add_separator() 102 | # To quit the notepad 103 | FileMenu.add_command(label="Exit", command=quitApp) 104 | 105 | MenuBar.add_cascade(label="File", menu=FileMenu) 106 | 107 | # Edit Menu 108 | EditMenu = Menu(MenuBar, tearoff=0) 109 | # To give a feature of Cut, Copy, Paste 110 | EditMenu.add_command(label="Cut", command=cut) 111 | EditMenu.add_command(label="Copy", command=copy) 112 | EditMenu.add_command(label="Paste", command=paste) 113 | 114 | MenuBar.add_cascade(label="Edit", menu=EditMenu) 115 | 116 | # Help Menu 117 | HelpMenu = Menu(MenuBar, tearoff=0) 118 | HelpMenu.add_command(label="About Notepad", command=about) 119 | 120 | MenuBar.add_cascade(label="Help", menu=HelpMenu) 121 | 122 | # ---------------- Creating a Text Area ---------------- 123 | # Text area for writing text 124 | TextArea = Text(root, font="lucida 13") 125 | file = None 126 | TextArea.pack(expand=True, fill=BOTH) 127 | 128 | # Adding Scrollbar using rules from tkinter 129 | scroll = Scrollbar(TextArea) 130 | scroll.pack(side=RIGHT, fill=Y) 131 | scroll.config(command=TextArea.yview) 132 | TextArea.config(yscrollcommand=scroll.set) 133 | 134 | # ---------------- Creating a Bottom Status Bar ---------------- 135 | # line_col = StringVar() 136 | # line_col.set("Ln 1, Col 1") 137 | 138 | statusbar = Frame(root, bd=1, relief=SUNKEN) 139 | statusbar.pack(side=BOTTOM, fill=X) 140 | 141 | Label(statusbar, text="UTF-8", width=20).pack(side=RIGHT) 142 | Label(statusbar, text="Windows(CRLF)", width=20).pack(side=RIGHT) 143 | # Label(statusbar, textvariable=line_col, width=20).pack(side=RIGHT) 144 | 145 | root.mainloop() 146 | --------------------------------------------------------------------------------