0

I'm trying to test a calendar I'll be using in a bigger aspect of a program, but I'm having trouble even getting it to work. I'm using python 2.7 (before anyone asks me to upgrade to 3.x, I'm using it for reasons of also using older libraries, and it's only for a course project so has no real work applications) on Windows 10, and whatever I run the program in (PyScripter, IDLE), it keeps coming back saying:

ImportError: No module named tkinter

As far as I have come to understand, Tkinter is part of Python itself, so should not really be having this problem, even in 2.7.

The code I am using was actually used by someone else trying to get a calendar to function, and I used it and tweaked it so it would only show the weekdays (the program I'm making doesn't require them, so I'm trying to omit them from the code), but as I can't even get it to run, I'm at a loss of how to make it go. If anyone can even suggest how to go about another way to make a calendar gui not show weekends, or even just grey them out so they'd not be selectable, I'd be grateful for that, too

Here is the code I'm testing:

from tkinter import *
from calendar import *


class Application(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()

        year = int(raw_input('\nEnter Year eg. 2017\n'))
        month = int(raw_input('\nEnter month number.\n'))
        self.create_widgets(year, month)

    def create_widgets(self, year, month):

         days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
#create labels
    for i in range(7):
        label = Label(self, text=days[i])
        label.grid(row = 0, column = i)

    weekday, numDays = monthrange(year, month)
    week = 1
    for i in range(1, numDays + 1):
        button = Button(self, text = str(i))
        button.grid(row = week, column = weekday)

        weekday += 1
        if weekday > 4:
            week += 1
            weekday = 0


mainWindow = tk()
obj = Application(root)
mainWindow.mainloop()
Gordon
  • 1
  • I think in Python 2.7 the module is `Tkinter` with a capital 'T'. In Python 3.x it was renamed to `tkinter`. – Jason Apr 16 '17 at 08:28
  • When I change `tkinter` to `Tkinter`, I get the following error now: `Traceback (most recent call last): File "C:\Users\Gordon\Desktop\new 4.py", line 35, in mainWindow = tk() NameError: name 'tk' is not defined` – Gordon Apr 16 '17 at 08:50
  • Try changing `tk()` to `Tk()`. ;-) – Jason Apr 16 '17 at 08:54
  • The problem now is that I'm not sure where I should put a root – Gordon Apr 16 '17 at 08:55
  • `Traceback (most recent call last): File "C:\Users\Gordon\Desktop\new 4.py", line 36, in obj = Application(root) NameError: name 'root' is not defined` – Gordon Apr 16 '17 at 08:56
  • In plain English, the error above is because you've passed a variable called `root` to your `Application` class. The problem is you haven't created any variable called `root`. You'll need to go back to the code you're using as a template and look for the variable `root` being created. – Jason Apr 16 '17 at 09:00
  • I've figured it out now. Thanks for the help, I seem to have gotten it thanks to what you've said :) – Gordon Apr 16 '17 at 09:03
  • At some point before you call `obj = Application(root)`, you'll need to have a line on the top level (i.e. not inside your class) that says `root = [whatever root should be]` – Jason Apr 16 '17 at 09:03

0 Answers0