Question

I'm trying to make a simplified calendar widget in Tkinter/ttk to allow me to select month and year. I fail to get all of my elements to show. In the form given below, it only shows the combobox with the months. I'm sure, I'm just making some stupid error that someone else will point out instantly, and I will then proceed to facepalm at my stupidity :-). The goal is to have the buttons, the entry and the combobox all show in the configuration:

| <Button | Entry | >Button |
|          Combobox         |
|        ChooseButton       |

If I uncomment self.pack(), it hangs. If I uncomment self.pack() AND comment away self.box.grid, it shows the rest of the widgets. Now, I assume I'm not supposed to used pack and grid together, I just tried it because I saw other people do it and I figured I might just as well give it a try.

from Tkinter import Tk
from ttk import Frame, Combobox, Button, Entry
import calendar, re

def printThisFunc(string):
    def printFunc():
        print string
    return printFunc

class SelectMonthDialog(Frame):
    def __init__(self, parent, startDate):
        Frame.__init__(self, parent, padding='10px')   
        self.parent = parent
        self.parent.title('Select month')
        button = Button(self, text='<', command = printThisFunc('<'))
        button.grid(row=0, column=0)
        self.yearEntry = Entry(self)
        self.yearEntry.insert(0,str(startDate.year))
        self.yearEntry.grid(row=0, column=1)
        button = Button(self, text='>', command = printThisFunc('>'))
        button.grid(row=0, column=2)
        months = calendar.month_name[1:]
        self.box = Combobox(self.parent, state='readonly')
        self.box['values'] = months
        self.box.current(startDate.month-1)
        self.box.grid(row=1, column=0, columnspan=3)
        button = Button(self, text='Choose', command = printThisFunc('choose'))
        button.grid(row=2, column=0, columnspan=3)
        #self.pack()

if __name__ == "__main__":
    from datetime import datetime
    root = Tk()
    SelectMonthDialog(root, datetime.today())
    root.mainloop()
Was it helpful?

Solution

Most of the widgets you create have a parent of self. That means they are children of the widget that is an instance of SelectMonthDialog. However, you don't ever pack/place/grid that frame in its parent, so it and all of its children remain invisible.

The reason the combobox shows up is because its parent is the parent widget.

The solution is to:

  1. make the parent of the combobox be self so it becomes part of the SelectMonthDialog frame
  2. pack self in its parent so that it is visible

OTHER TIPS

For some elements you set self as parent for others you set self.parent as parent - use only one of them.

If you use self.parent you put elements inside root and you don't need self (Frame). Using self.pack() you probably replace all elements in root with Frame.

If you use self you put elements inside Frame and you have to put Frame inside root so you need self.pack()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top