4
\$\begingroup\$

I made a calendar that displays text documents according to month. Thankfully it works, although I could really use some help how to make it more better looking and efficient. I'm new to programming but I have horrible organization skills, and can't really understand the concept of classes, and __init__.

from Tkinter import *

months = [
    "January", "Febuary", "March", "April", "May",
    "June", "July", "August", "September", "October",
    "November", "December" 
]

January = [
        27, 28, 29, 30, 31, 1, 2,
        3, 4, 5, 6, 7, 8, 9, 
        10, 11, 12, 13, 14, 15, 16, 
        17, 18, 19, 20, 21, 22, 23, 
        24, 25, 26, 27, 28, 29, 30, 
        31, 1, 2, 3, 4, 5, 6
]

Febuary = [
        31, 1, 2, 3, 4, 5, 6,
        7, 8, 9, 10, 11, 12, 13,
        14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27,
        28, 29, 1, 2, 3, 4, 5,
        6, 7, 8, 9, 10, 11, 12
]

March = [
    28, 29, 1, 2, 3, 4, 5,
    6, 7, 8, 9, 10, 11, 12,
    13, 14, 15, 16, 17, 18, 19,
    20, 21, 22, 23, 24, 25, 26,
    27, 28, 29, 30, 31, 1, 2,
    3, 4, 5, 6, 7, 8, 9
]

April = [
    27, 28, 29, 30, 31, 1, 2,
    3, 4, 5, 6, 7, 8, 9,
    10, 11, 12, 13, 14, 15, 16,
    17, 18, 19, 20, 21, 22, 23,
    24, 25, 26, 27, 28, 29, 30,
    1, 2, 3, 4, 5, 6, 7
]   

May = [
    24, 25, 26, 27, 28, 29, 30,
    1, 2, 3, 4, 5, 6, 7,
    8, 9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21,
    22, 23, 24, 25, 26, 27, 28,
    29, 30, 31, 1, 2, 3, 4
]

June = [
    29, 30, 31, 1, 2, 3, 4,
    5, 6, 7, 8, 9, 10, 11,
    12, 13, 14, 15, 16, 17, 18,
    19, 20, 21, 22, 23, 24, 25,
    26, 27, 28, 29, 30, 1, 2,
    3, 4, 5, 6, 7, 8, 9
]

July = [
    26, 27, 28, 29, 30, 1, 2,
    3, 4, 5, 6, 7, 8, 9,
    10, 11, 12, 13, 14, 15, 16,
    17, 18, 19, 20, 21, 22, 23,
    24, 25, 26, 27, 28, 29, 30,
    31, 1, 2, 3, 4, 5, 6
]

August = [
    31, 1, 2, 3, 4, 5, 6,
    7, 8, 9, 10, 11, 12, 13,
    14, 15, 16, 17, 18, 19, 20,
    21, 22, 23, 24, 25, 26, 27,
    28, 29, 30, 31, 1, 2, 3,
    4, 5, 6, 7, 8, 9, 10
]

September = [
    28, 29, 30, 31, 1, 2, 3,
    4, 5, 6, 7, 8, 9, 10,
    11, 12, 13, 14, 15, 16, 17,
    18, 19, 20, 21, 22, 23, 24,
    25, 26, 27, 28, 29, 30, 1,
    2, 3, 4, 5, 6, 7, 8
]

October = [
    25, 26, 27, 28, 29, 30, 1,
    2, 3, 4, 5, 6, 7, 8,
    9, 10, 11, 12, 13, 14, 15,
    16, 17, 18, 19, 20, 21, 22,
    23, 24, 25, 26, 27, 28, 29,
    30, 31, 1, 2, 3, 4, 5
]

November = [
    30, 31, 1, 2, 3, 4, 5,
    6, 7, 8, 9, 10, 11, 12,
    13, 14, 15, 16, 17, 18, 19, 
    20, 21, 22, 23, 24, 25, 26,
    27, 28, 29, 30, 1, 2, 3, 
    4, 5, 6, 7, 8, 9, 10
]

December = [
    27, 28, 29, 30, 1, 2, 3,
    4, 5, 6, 7, 8, 9, 10,
    11, 12, 13, 14, 15, 16, 17,
    18, 19, 20, 21, 22, 23, 24,
    25, 26, 27, 28, 29, 30, 31,
    1, 2, 3, 4, 5, 6, 7
]

ht = 2
bg1 = "blue"
fg1 = "white"
current_year = "2016"

def replace(month, operation): # "+" or "-"
    if operation == "+":
        if months.index(month) == 11:
            return months[0]
        else:   
            return months[months.index(month)+1]
    elif operation == "-":
        return months[months.index(month)-1]

def replace_back(event):        
    l1.configure(text=replace(l1["text"], "-"))

    if l1["text"] == "Febuary":
        change_month(Febuary)
        load()
    elif l1["text"] == "March":
        change_month(March)
        load()
    elif l1["text"] == "April":
        change_month(April)
        load()
    elif l1["text"] == "May":
        change_month(May)
        load()
    elif l1["text"] == "June":
        change_month(June)
        load()
    elif l1["text"] == "July":
        change_month(July)
        load()
    elif l1["text"] == "August":
        change_month(August)
        load()
    elif l1["text"] == "September":
        change_month(September)
        load()
    elif l1["text"] == "October":
        change_month(October)
        load()
    elif l1["text"] == "November":
        change_month(November)
        load()
    elif l1["text"] == "December":
        change_month(December)
        load()
    else:
        change_month(January)
        load()

def replace_back2():        
    l1.configure(text=replace(l1["text"], "-"))

    if l1["text"] == "Febuary":
        change_month(Febuary)
        load()
    elif l1["text"] == "March":
        change_month(March)
        load()
    elif l1["text"] == "April":
        change_month(April)
        load()
    elif l1["text"] == "May":
        change_month(May)
        load()
    elif l1["text"] == "June":
        change_month(June)
        load()
    elif l1["text"] == "July":
        change_month(July)
        load()
    elif l1["text"] == "August":
        change_month(August)
        load()
    elif l1["text"] == "September":
        change_month(September)
        load()
    elif l1["text"] == "October":
        change_month(October)
        load()
    elif l1["text"] == "November":
        change_month(November)
        load()
    elif l1["text"] == "December":
        change_month(December)
        load()
    else:
        change_month(January)
        load()

def replace_forward(event):
    l1.configure(text=replace(l1["text"], "+"))

    if l1["text"] == "Febuary":
        change_month(Febuary)
        load()
    elif l1["text"] == "March":
        change_month(March)
        load()
    elif l1["text"] == "April":
        change_month(April)
        load()
    elif l1["text"] == "May":
        change_month(May)
        load()
    elif l1["text"] == "June":
        change_month(June)
        load()
    elif l1["text"] == "July":
        change_month(July)
        load()
    elif l1["text"] == "August":
        change_month(August)
        load()
    elif l1["text"] == "September":
        change_month(September)
        load()
    elif l1["text"] == "October":
        change_month(October)
        load()
    elif l1["text"] == "November":
        change_month(November)
        load()
    elif l1["text"] == "December":
        change_month(December)
        load()
    else:
        change_month(January)
        load()

def replace_forward2():
    l1.configure(text=replace(l1["text"], "+"))

    if l1["text"] == "Febuary":
        change_month(Febuary)
        load()
    elif l1["text"] == "March":
        change_month(March)
        load()
    elif l1["text"] == "April":
        change_month(April)
        load()
    elif l1["text"] == "May":
        change_month(May)
        load()
    elif l1["text"] == "June":
        change_month(June)
        load()
    elif l1["text"] == "July":
        change_month(July)
        load()
    elif l1["text"] == "August":
        change_month(August)
        load()
    elif l1["text"] == "September":
        change_month(September)
        load()
    elif l1["text"] == "October":
        change_month(October)
        load()
    elif l1["text"] == "November":
        change_month(November)
        load()
    elif l1["text"] == "December":
        change_month(December)
        load()
    else:
        change_month(January)
        load()

def edit():
    text1.configure(state="normal")
    vb2.configure(state="normal")
    vb3.configure(state="normal")
    root.after(2000, normalize)

def normalize():
    vb2.configure(state="disabled")
    vb3.configure(state="disabled")

def save():
    s = open("Months 2016/"+l1["text"]+".txt", "w")
    text = text1.get(1.0, END)
    try:
        s.write(text.rstrip())
        s.write("\n")
    finally:
        s.close()

def load():
    text1.configure(state="normal")
    l2.configure(text=l1["text"]+" 2016")
    text = open("Months 2016/"+l1["text"]+".txt").read()
    text1.delete(1.0, END)
    text1.insert(END, text)
    text1.mark_set(INSERT, 1.0)
    text1.configure(state="disabled")

root = Tk()

root.geometry("650x400")
root.focus_set()

top1_frame = Frame(root)
top1_frame.pack(expand=1, fill=BOTH)

bottom1_frame = Frame(root)
bottom1_frame.pack(expand=1, fill=BOTH)

left_frame = Frame(top1_frame)
left_frame.pack(side=LEFT, expand=1, fill=BOTH)

right_frame = Frame(top1_frame)
right_frame.pack(side=LEFT, expan=1, fill=BOTH)

Button(bottom1_frame, text="Testing...").pack(expand=1, fill=BOTH)

l2 = Label(right_frame, text="January 2016")
l2.pack(expand=1, fill=BOTH)
text1 = Text(right_frame, height=15, width=30)
text1.pack(expand=1, fill=Y)

bottom1_frame2 = Frame(right_frame)
bottom1_frame2.pack(side=BOTTOM, expand=1, fill=BOTH)

vb1 = Button(bottom1_frame2, text="Edit", command=lambda: edit())
vb2 = Button(bottom1_frame2, text="Save", state="disabled", command=lambda: save())
vb3 = Button(bottom1_frame2, text="Cancel", state="disabled", command=lambda: load())

vb1.pack(side=LEFT, expand=1, fill=X)
vb2.pack(side=LEFT, expand=1, fill=X)
vb3.pack(side=LEFT, expand=1, fill=X)

jan = Frame(left_frame)
jan.pack(side=TOP, fill=X)
frame_days = Frame(left_frame)
frame_days.pack(expand=1, fill=BOTH)

root.bind("<Left>", replace_back)
root.bind("<Right>", replace_forward)

b1 = Button(jan, text="<", command=replace_back2)
b1.pack(side=LEFT, expand=1, fill=BOTH)
l1 = Label(jan, text="January", font="Courier 24", bg="blue", fg="white")
l1.pack(side=LEFT, expand=1, fill=X)
b2 = Button(jan, text=">", command=replace_forward2)
b2.pack(side=LEFT, expand=1, fill=BOTH)

load()

Label(frame_days, text="Sunday").pack(side=LEFT, expand=1, fill=X)
Label(frame_days, text="Monday").pack(side=LEFT, expand=1, fill=X)
Label(frame_days, text="Tuesday").pack(side=LEFT, expand=1, fill=X)
Label(frame_days, text="Wednesday").pack(side=LEFT, expand=1, fill=X)
Label(frame_days, text="Thursday").pack(side=LEFT, expand=1, fill=X)
Label(frame_days, text="Friday").pack(side=LEFT, expand=1, fill=X)
Label(frame_days, text="Saturday").pack(side=LEFT, expand=1, fill=X)

frame_wk1 = Frame(left_frame)
frame_wk2 = Frame(left_frame)
frame_wk3 = Frame(left_frame)
frame_wk4 = Frame(left_frame)
frame_wk5 = Frame(left_frame)
frame_wk6 = Frame(left_frame)

frame_wk1.pack(expand=1, fill=BOTH)
frame_wk2.pack(expand=1, fill=BOTH)
frame_wk3.pack(expand=1, fill=BOTH)
frame_wk4.pack(expand=1, fill=BOTH)
frame_wk5.pack(expand=1, fill=BOTH)
frame_wk6.pack(expand=1, fill=BOTH)

b1 = Button(frame_wk1, text=January[0], height=ht, width=5, state="disabled")
b2 = Button(frame_wk1, text=January[1], height=ht, width=5, state="disabled")
b3 = Button(frame_wk1, text=January[2], height=ht, width=5, state="disabled")
b4 = Button(frame_wk1, text=January[3], height=ht, width=5, state="disabled")
b5 = Button(frame_wk1, text=January[4], height=ht, width=5, state="disabled")
b6 = Button(frame_wk1, text=January[5], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[5])+"\n-\n"))
b7 = Button(frame_wk1, text=January[6], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[6])+"\n-\n"))

b8 = Button(frame_wk2, text=January[7], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[7])+"\n-\n"))
b9 = Button(frame_wk2, text=January[8], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[8])+"\n-\n"))
b10 = Button(frame_wk2, text=January[9], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[9])+"\n-\n"))
b11 = Button(frame_wk2, text=January[10], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[10])+"\n-\n"))
b12 = Button(frame_wk2, text=January[11], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[11])+"\n-\n"))
b13 = Button(frame_wk2, text=January[12], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[12])+"\n-\n"))
b14 = Button(frame_wk2, text=January[13], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[13])+"\n-\n"))

b15 = Button(frame_wk3, text=January[14], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[14])+"\n-\n"))
b16 = Button(frame_wk3, text=January[15], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[15])+"\n-\n"))
b17 = Button(frame_wk3, text=January[16], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[16])+"\n-\n"))
b18 = Button(frame_wk3, text=January[17], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[17])+"\n-\n"))
b19 = Button(frame_wk3, text=January[18], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[18])+"\n-\n"))
b20 = Button(frame_wk3, text=January[19], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[19])+"\n-\n"))
b21 = Button(frame_wk3, text=January[20], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[20])+"\n-\n"))

b22 = Button(frame_wk4, text=January[21], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[21])+"\n-\n"))
b23 = Button(frame_wk4, text=January[22], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[22])+"\n-\n"))
b24 = Button(frame_wk4, text=January[23], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[23])+"\n-\n"))
b25 = Button(frame_wk4, text=January[24], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[24])+"\n-\n"))
b26 = Button(frame_wk4, text=January[25], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[25])+"\n-\n"))
b27 = Button(frame_wk4, text=January[26], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[26])+"\n-\n"))
b28 = Button(frame_wk4, text=January[27], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[27])+"\n-\n"))

b29 = Button(frame_wk5, text=January[28], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[28])+"\n-\n"))
b30 = Button(frame_wk5, text=January[29], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[29])+"\n-\n"))
b31 = Button(frame_wk5, text=January[30], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[30])+"\n-\n"))
b32 = Button(frame_wk5, text=January[31], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[31])+"\n-\n"))
b33 = Button(frame_wk5, text=January[32], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[32])+"\n-\n"))
b34 = Button(frame_wk5, text=January[33], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[33])+"\n-\n"))
b35 = Button(frame_wk5, text=January[34], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[34])+"\n-\n"))

b36 = Button(frame_wk6, text=January[35], height=ht, width=5, bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(January[35])+"\n-\n"))
b37 = Button(frame_wk6, text=January[36], height=ht, width=5, state="disabled")
b38 = Button(frame_wk6, text=January[37], height=ht, width=5, state="disabled")
b39 = Button(frame_wk6, text=January[38], height=ht, width=5, state="disabled")
b40 = Button(frame_wk6, text=January[39], height=ht, width=5, state="disabled")
b41 = Button(frame_wk6, text=January[40], height=ht, width=5, state="disabled")
b42 = Button(frame_wk6, text=January[41], height=ht, width=5, state="disabled")

b1.pack(side=LEFT, expand=1, fill=BOTH)
b2.pack(side=LEFT, expand=1, fill=BOTH) 
b3.pack(side=LEFT, expand=1, fill=BOTH) 
b4.pack(side=LEFT, expand=1, fill=BOTH) 
b5.pack(side=LEFT, expand=1, fill=BOTH) 
b6.pack(side=LEFT, expand=1, fill=BOTH) 
b7.pack(side=LEFT, expand=1, fill=BOTH) 
b8.pack(side=LEFT, expand=1, fill=BOTH) 
b9.pack(side=LEFT, expand=1, fill=BOTH) 
b10.pack(side=LEFT, expand=1, fill=BOTH) 
b11.pack(side=LEFT, expand=1, fill=BOTH) 
b12.pack(side=LEFT, expand=1, fill=BOTH) 
b13.pack(side=LEFT, expand=1, fill=BOTH) 
b14.pack(side=LEFT, expand=1, fill=BOTH) 
b15.pack(side=LEFT, expand=1, fill=BOTH) 
b16.pack(side=LEFT, expand=1, fill=BOTH) 
b17.pack(side=LEFT, expand=1, fill=BOTH) 
b18.pack(side=LEFT, expand=1, fill=BOTH) 
b19.pack(side=LEFT, expand=1, fill=BOTH) 
b20.pack(side=LEFT, expand=1, fill=BOTH) 
b21.pack(side=LEFT, expand=1, fill=BOTH) 
b22.pack(side=LEFT, expand=1, fill=BOTH) 
b23.pack(side=LEFT, expand=1, fill=BOTH) 
b24.pack(side=LEFT, expand=1, fill=BOTH) 
b25.pack(side=LEFT, expand=1, fill=BOTH) 
b26.pack(side=LEFT, expand=1, fill=BOTH) 
b27.pack(side=LEFT, expand=1, fill=BOTH) 
b28.pack(side=LEFT, expand=1, fill=BOTH) 
b29.pack(side=LEFT, expand=1, fill=BOTH) 
b30.pack(side=LEFT, expand=1, fill=BOTH) 
b31.pack(side=LEFT, expand=1, fill=BOTH) 
b32.pack(side=LEFT, expand=1, fill=BOTH) 
b33.pack(side=LEFT, expand=1, fill=BOTH) 
b34.pack(side=LEFT, expand=1, fill=BOTH) 
b35.pack(side=LEFT, expand=1, fill=BOTH) 
b36.pack(side=LEFT, expand=1, fill=BOTH) 
b37.pack(side=LEFT, expand=1, fill=BOTH) 
b38.pack(side=LEFT, expand=1, fill=BOTH) 
b39.pack(side=LEFT, expand=1, fill=BOTH) 
b40.pack(side=LEFT, expand=1, fill=BOTH) 
b41.pack(side=LEFT, expand=1, fill=BOTH) 
b42.pack(side=LEFT, expand=1, fill=BOTH)

def upper_check(button, new_months):
    if int(new_months) < 20:
        button.configure(bg=bg1, fg=fg1, state="normal", command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months)+"\n-\n"))
    elif int(new_months) > 20:
        button.configure(bg="white", fg="black", state="disabled")

def lower_check(button, new_months):
    if int(new_months) > 20:
        button.configure(bg=bg1, fg=fg1, state="normal", command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months)+"\n-\n"))
    elif int(new_months) < 20:
        button.configure(bg="white", fg="black", state="disabled")

def change_month(new_months):
    month = str(new_months)

    b1.configure(text=new_months[0])
    upper_check(b1, new_months[0])
    b2.configure(text=new_months[1])
    upper_check(b2, new_months[1])
    b3.configure(text=new_months[2])
    upper_check(b3, new_months[2])
    b4.configure(text=new_months[3])
    upper_check(b4, new_months[3])
    b5.configure(text=new_months[4])
    upper_check(b5, new_months[4])
    b6.configure(text=new_months[5])
    upper_check(b6, new_months[5])
    b7.configure(text=new_months[6])
    upper_check(b7, new_months[6])

    b8.configure(text=new_months[7], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[7])+"\n-\n"))
    b9.configure(text=new_months[8], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[8])+"\n-\n"))
    b10.configure(text=new_months[9], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[9])+"\n-\n"))
    b11.configure(text=new_months[10], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[10])+"\n-\n"))
    b12.configure(text=new_months[11], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[11])+"\n-\n"))
    b13.configure(text=new_months[12], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[12])+"\n-\n"))
    b14.configure(text=new_months[13], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[13])+"\n-\n"))

    b15.configure(text=new_months[14], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[14])+"\n-\n"))
    b16.configure(text=new_months[15], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[15])+"\n-\n"))
    b17.configure(text=new_months[16], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[16])+"\n-\n"))
    b18.configure(text=new_months[17], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[17])+"\n-\n"))
    b19.configure(text=new_months[18], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[18])+"\n-\n"))
    b20.configure(text=new_months[19], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[19])+"\n-\n"))
    b21.configure(text=new_months[20], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[20])+"\n-\n"))

    b22.configure(text=new_months[21], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[21])+"\n-\n"))
    b23.configure(text=new_months[22], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[22])+"\n-\n"))
    b24.configure(text=new_months[23], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[23])+"\n-\n"))
    b25.configure(text=new_months[24], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[24])+"\n-\n"))
    b26.configure(text=new_months[25], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[25])+"\n-\n"))
    b27.configure(text=new_months[26], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[26])+"\n-\n"))
    b28.configure(text=new_months[27], bg=bg1, fg=fg1, command=lambda: text1.insert(1.0, l1["text"]+" "+str(new_months[27])+"\n-\n"))

    b29.configure(text=new_months[28])
    lower_check(b29, new_months[28])
    b30.configure(text=new_months[29])
    lower_check(b30, new_months[29])
    b31.configure(text=new_months[30])
    lower_check(b31, new_months[30])
    b32.configure(text=new_months[31])
    lower_check(b32, new_months[31])
    b33.configure(text=new_months[32])
    lower_check(b33, new_months[32])
    b34.configure(text=new_months[33])
    lower_check(b34, new_months[33])
    b35.configure(text=new_months[34])
    lower_check(b35, new_months[34])

    b36.configure(text=new_months[35])
    lower_check(b36, new_months[35])
    b37.configure(text=new_months[36])
    lower_check(b37, new_months[36])
    b38.configure(text=new_months[37])
    lower_check(b38, new_months[37])
    b39.configure(text=new_months[38])
    lower_check(b39, new_months[38])
    b40.configure(text=new_months[39])
    lower_check(b40, new_months[39])
    b41.configure(text=new_months[40])
    lower_check(b41, new_months[40])
    b42.configure(text=new_months[41])
    lower_check(b42, new_months[41])

root.mainloop()
\$\endgroup\$
2
  • 4
    \$\begingroup\$ Welcome to Code Review! I'm sure our Python contributors will have some great ideas to improve your code. \$\endgroup\$ – Phrancis Mar 20 '16 at 2:51
  • 3
    \$\begingroup\$ No time for a proper review, so... Ever heard of the calendar module? \$\endgroup\$ – 301_Moved_Permanently Mar 20 '16 at 8:49
7
\$\begingroup\$

From PEP8:

Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.

You import Tkinter like that. Instead, use import Tkinter as tk and use tk. before each Tkinter variable.

January = [
    27, 28, 29, 30, 31, 1, 2,
    ...

That code is very repetitive. Probably the easiest way would be to say something like:

ranges = {}
ranges['January'] = ((27, 32), (1, 32), (1, 7))
ranges['February'] = ((31, 32), (1, 30), (1, 13))
...
calendars = {}
for month, start_ends in ranges:
    calendars[month] = sum((list(range(*start_end)) for start_end in start_ends), [])

Then instead of using January, you could use calendars['January']. It also shortens your code by quite a bit.

ht = 2
bg1 = "blue"
...

Those look like they are constants. From PEP8:

Constants are usually defined on a module level and written in all capital letters with underscores separating words. Examples include MAX_OVERFLOW and TOTAL.

Taking from that, they should be HT, BG1, FG1, and CURRENT_YEAR.

def replace(month, operation):

The name replace makes it look like it is actually doing some replacing, but the code doesn't look like that. It looks like you are getting an adjacent month not replacing anything. I would give it some more appropriate name. Also, you should use a number for operation instead of a string:

def adjacent(month, shift=1):
    return months[(months.index(month) + shift) % len(months]

That way, you can use adjacent(month, -1) to get the previous month, and adjacent(month) to get the next month. You should read more about the modulus operator.

if l1["text"] == "February": # I corrected your spelling mistake
    change_month(February)
    load()
elif l1["text"] == "March":
    ...

If we had left the calendars as separate variables like you originally defined them, my fault would be that you call load() once for each if and elif statement. Since they are all calling it, you should just put that at the end. We aren't doing that, though, so you can replace all of those if and elif statements with this:

change_month(months.get(l1["text"], "January")) # I used "January" as the default since you used that month in your else block.
load()

That dramatically cuts the number of lines.

replace_back(), replace_back2(), replace_forward(), and replace_forward2() all do the exact same thing. replace_back() and replace_forward() take the event argument, but the actual code part of the functions are all identical. That means that you can put them all into one. If you want to be able to take an event argument, but you want to be able not to, use def replace(event=None):

s = open("Months 2016/" + l1["text"] + ".txt", "w")

I have two problems with that: you should be using a with block and you should be using string formatting:

with open("Months 2016/{month}.txt".format(month=l1["text"]), "w") as s:
    text = text1.get(1.0, END)
    s.write(text.rstrip() + "\n")

You don't need try and finally when you use a with block.

Label(frame_days, text="Sunday").pack(side=LEFT, expand=1, fill=X)
Label(frame_days, text="Monday").pack(...)
...

Look at how repetitive that is! You should use a loop:

for day in ("Sunday", "Monday", "Tuesday", "Wednesday", ...):
    tk.Label(frame_days, text=day).pack(side=tk.LEFT, expand=1, fill=tk.X)

But more repetition is found here:

frame_wk1 = Frame(left_frame)
framw_wk2 = Frame(left_frame)
...
b1 = Button(frame_wk1, text=January[0], ...)
b2 = Button(frame_wk1, text=January[1], ...)
...
b1.pack(side=LEFT, expand=1, fill=BOTH)
b2.pack(side=LEFT, expand=1, fill=BOTH)
...

You have about 40 lines that are almost identical. That is a definite sign that you should be using a loop:

buttons = []
january = ranges['January']
before_and_after = list(range(*january[0])) + list(range(*january[-1])) # Days that are not actually in this month
calendar = months['January']
for j in range(len(calendar)):
    if not j % 7: # If j % 7 is equal to 0.  That way, we create a new frame for every seven j's.
        frame = tk.Frame(left_frame)
        frame.pack(expand=1, fill=tk.BOTH)
    keywords = {
        'text': calendar[0]
        'height': ht,
        'width': 5,
    }

    if j in before_and_after:
        keywords.update({
            'state': 'disabled'
        }
    else:
        keywords.update({
            'bg': BG1,
            'fg': FG1,
            'command': lambda k=j: text1.insert(1.0, "{} {}\n-\n".format(l1["text"], calendar[k])),
        }
    button = tk.Button(frame, **keywords)
    button.pack(side=tk.LEFT, expand=1, fill=tk.BOTH)
    buttons.append(button)

I haven't actually tested that code, but it should at least give you the idea.

b1.configure(text=new_months[0])
upper_check(b1, new_months[0])
b2.configure(text=new_months[1])
upper_check(b2, new_months[1])
...

Yet again we have repetition. In the code above I created a list called buttons, so we can use that here:

def change_month(new_months):
    before = list(range(*january[0]))
    after = list(range(*january[-1]))
    for i, (button, month) in enumerate(zip(buttons, new_months)):
        in_before = i in before
        in_after = i in after:
        if in_before or in_after:
            button.configure(text=month)
        if in_before:
            upper_check(button, month)
        elif in_after:
            lower_check(button, month)
        else:
            button.configure(
                text=month, bg=BG1, fg=FG1,
                command=lambda: text1.insert(1.0, "{} {}\n-\n".format(l1["text"], month))
            )

The lesson to be learned from this: if it looks repetitive, you can probably solve it with a loop.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.