Tkinter question

Chad Netzer cnetzer at mail.arc.nasa.gov
Thu Apr 3 18:20:59 EST 2003


On Thu, 2003-04-03 at 01:14, Ali Dada wrote:
> hi all:
> 
> i have a window made of a couple of buttons stacked above each other.i need to
> any a widget on the top that opens a submenu to the right when clicked. how can
> i do that? see the code below and tell me how to correct it (the submenu is
> opening downwards over the other buttons, not to the right)

Well, since you asked so nicely...

You are trying to have a menubar menu open to the right rather than
down, which I don't think is possible with Tk (or if it is, it is a lot
of kludgery).

However, you may be able to use a menubutton, instead.  This will have
different semantics than a Menu in the menubar, but it allows you to
specify the direction you want a pop-up menu to open.

Here is my reworked version of your code, to show what I mean.  (one
tip- do not pack an derived frame widget in it's own init() method.  Let
the code that created that derived frame decide how and where to pack
it.)


#!/usr/bin/env python
import sys
from Tkinter import *
from tkMessageBox import *
from tkSimpleDialog import askfloat

class Index(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.makemenu(self)
        Button(self, text='Repository').pack(side=TOP, fill=BOTH)
        Button(self, text='Regression').pack(side=TOP, fill=BOTH)
        Button(self, text='Code Manage').pack(side=TOP, fill=BOTH)

    def makemenu(self, parent):
        pref = Menubutton(parent,
                          text='Preference >',
                          underline=0,
                          direction='right')

        pref1 = Menu(pref, tearoff=0)
        pref1.add_command(label='Cut', underline=0)
        pref1.add_command(label='Paste', underline=0)
        pref.configure(menu=pref1)
        pref.pack(side=TOP)

if __name__ == '__main__':
    root=Tk()
    root.title('TAGZ')

    i = Index()
    # NOTE - do NOT do a self.pack() in __init__.  That is changing
    # the behavior of the widget you are inheriting, and is very bad
    # form.
    i.pack()

    i.mainloop()







More information about the Python-list mailing list