1
+ from tkinter import *
2
+ from tkinter import messagebox
3
+ #Define the root window
4
+ root = Tk ()
5
+ root .title ("My Checklist App" )
6
+ root .geometry ("400x400" )
7
+ root .resizable (0 ,0 )
8
+
9
+
10
+ #setting up app theme - font and colours
11
+ myFont = ("Times New Roman" ,12 )
12
+ rootColour = "#3b5998"
13
+ buttonColour = "#f7f7f7"
14
+
15
+ #changing root bg
16
+ root .config (bg = rootColour )
17
+
18
+ #FUNCTIONS
19
+ #ADD ITEM FUNCTION
20
+ def addItem ():
21
+ if listEntry .get () == "" :
22
+ messagebox .showinfo ("Illegal Entry" ,"Cannot Enter Blank Items" )
23
+ else :
24
+ listBox .insert (END ,listEntry .get ())
25
+ listEntry .delete (0 ,END )
26
+ #REMOVE ITEM FUNCTION
27
+ def removeItem ():
28
+ listBox .delete (ANCHOR )
29
+ #CLEAR LIST FUNCTION
30
+ def clearList ():
31
+ listBox .delete (0 ,END )
32
+
33
+ #SAVE LIST FUNCTION
34
+ def saveList ():
35
+ with open ('checklist.txt' ,'w' ) as f :
36
+ toSaveList = listBox .get (0 ,END )
37
+ #print(toSaveList)
38
+ for element in toSaveList :
39
+ if element .endswith ("\n " ):
40
+ f .write (element )
41
+ else :
42
+ f .write (element + "\n " )
43
+
44
+ #READING FROM FILE WHEN ALL REOPENS
45
+ def openList ():
46
+ try :
47
+ with open ('checklist.txt' ,'r' ) as f :
48
+ for line in f :
49
+ listBox .insert (END ,line )
50
+ except :
51
+ return
52
+
53
+ #defining the layout of the app
54
+
55
+ #FRAMES LAYOUT
56
+ inputFrame = Frame (root ,bg = rootColour )
57
+ outputFrame = Frame (root ,bg = rootColour )
58
+ buttonFrame = Frame (root ,bg = rootColour )
59
+ inputFrame .pack ()
60
+ outputFrame .pack ()
61
+ buttonFrame .pack ()
62
+
63
+ #LAYOUT OF INPUT FRAME ELEMENTS
64
+ listEntry = Entry (inputFrame ,width = 35 ,borderwidth = 3 ,font = myFont )
65
+ listAddButton = Button (inputFrame ,text = "Add Item" ,borderwidth = 2 ,font = myFont ,bg = buttonColour ,command = addItem )
66
+ listEntry .grid (row = 0 ,column = 0 ,padx = 5 ,pady = 5 )
67
+ listAddButton .grid (row = 0 ,column = 1 ,padx = 5 ,pady = 5 ,ipadx = 5 )
68
+
69
+ #LAYOUT OF OUTPUT FRAME
70
+ #Add scrollbar
71
+ scrollBar = Scrollbar (outputFrame )
72
+ listBox = Listbox (outputFrame ,height = 15 ,width = 45 ,borderwidth = 3 ,font = myFont ,yscrollcommand = scrollBar .set )
73
+ scrollBar .config (command = listBox .yview )
74
+ listBox .grid (row = 0 ,column = 0 )
75
+ scrollBar .grid (row = 0 ,column = 1 ,sticky = "NS" )
76
+
77
+
78
+ #LAYOUT OF BUTTON FRAME
79
+ listRemoveButton = Button (buttonFrame ,text = "Remove Item" ,borderwidth = 2 ,font = myFont ,bg = buttonColour ,command = removeItem )
80
+ listClearButton = Button (buttonFrame ,text = "Clear List" ,borderwidth = 2 ,font = myFont ,bg = buttonColour ,command = clearList )
81
+ saveButton = Button (buttonFrame ,text = "Save" ,borderwidth = 2 ,font = myFont ,bg = buttonColour ,command = saveList )
82
+ quitButton = Button (buttonFrame ,text = "Quit" ,command = root .destroy ,borderwidth = 2 ,font = myFont ,bg = buttonColour )
83
+ listRemoveButton .grid (row = 0 ,column = 0 ,padx = 2 ,pady = 10 )
84
+ listClearButton .grid (row = 0 ,column = 1 ,padx = 2 ,pady = 10 ,ipadx = 10 )
85
+ saveButton .grid (row = 0 ,column = 2 ,padx = 2 ,pady = 10 ,ipadx = 15 )
86
+ quitButton .grid (row = 0 ,column = 30 ,padx = 2 ,pady = 10 ,ipadx = 15 )
87
+ openList ()
88
+ root .mainloop ()
0 commit comments