hi , thanks for the code , I have changed it a little bit , I have also added two buttons (+,-)
I have written the code for (+) but it does not work , can you tell me what the problem is , the last lines which are commented out are supposed to show the following months but I keep getting error s .
thanks
#------------------------------------------------------------------------

from Tkinter import *
from calendar import *
y=2007
m=1
str1=month(y,m)
class MyApp:
def __init__(self,myparent):

self.mycontainer = Frame(myparent)
self.mycontainer.pack()
root.title("monthly calendar")


l2=Label(self.mycontainer,text= str1,justify="left",font=('courier',14,'bold'),bg='yellow')
l2.pack()
self.b1=Button(self.mycontainer,text='-',bg="green",padx=5,pady=5)
self.b1.pack(side=LEFT,ipadx=3,ipady=1)
self.b2=Button(self.mycontainer,text='+',bg="red",padx=5,pady=5)
self.b2.pack(side=RIGHT,ipadx=3,ipady=1)
self.b2.bind("<Button-1>",self.increase)
# def increase(self):

# if self.m==12:
# self.m=1
# self.y=self.y + 1
# else:
# self.m=self.m+1

if __name__ == '__main__':
root=Tk()
myapp=MyApp(root)
root.mainloop()

Recommended Answers

All 3 Replies

Several things:
Try to give Tkinter and Calendar a namespace, so you know where the functions come from.
Give variables names that don't confuse, like 'l1' looks a lot like '11' in many editors, switching to 'lb1' helps readability.

Please use the [code=python] and [/code] tag pair to enclose your python code.


Here is my suggestion:

# display a monthly calendar in Tkinter

import Tkinter as tk
import calendar as cd

class MyApp(object):
    def __init__(self, root, m, y):

        self.m = m
        self.y = y
        self.frame1 = tk.Frame(root)
        self.frame1.pack()
        str1 = cd.month(self.y, self.m)
        font1 = ('courier',14,'bold')
        self.lb2 = tk.Label(self.frame1, text=str1, font=font1, bg='yellow')
        self.lb2.pack()
        self.b1 = tk.Button(self.frame1,text=' - ', bg="green")
        self.b1.pack(side=tk.LEFT, padx=3, pady=3)
        self.b2 = tk.Button(self.frame1,text=' + ',bg="red")
        self.b2.pack(side=tk.RIGHT, padx=3, pady=3)
        
        self.b1.bind("<Button-1>", self.decrease)
        self.b2.bind("<Button-1>", self.increase)
        
        root.mainloop()

    def increase(self, e):
        if self.m == 12:
            self.m = 1
            self.y = self.y + 1
        else:
            self.m = self.m + 1
        self.lb2['text'] = cd.month(self.y, self.m)

    def decrease(self, e):
        if self.m == 1:
            self.m = 12
            self.y = self.y - 1
        else:
            self.m = self.m - 1
        self.lb2['text'] = cd.month(self.y, self.m)


if __name__ == '__main__':
    y = 2007
    m = 1
    root = tk.Tk()
    root.title("monthly calendar")
    myapp = MyApp(root, m, y)

hi , thank you very much , I know where I went wrong , I am going to play around with the code to see what I will get

You also complicate your code (and life) by switching to OOP, when python gives you the freedom not to do that with simple programs like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.