blob: cb75d85c7693a6f5b06bcd040aae48670877b787 [file] [log] [blame]
Martin v. Löwis8ec03e02002-03-17 18:19:13 +00001# -*-mode: python; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00002#
Tim Peters182b5ac2004-07-18 06:16:08 +00003# $Id$
Martin v. Löwis8ec03e02002-03-17 18:19:13 +00004#
5# Tix Demostration Program
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00006#
7# This sample program is structured in such a way so that it can be
Martin v. Löwis8ec03e02002-03-17 18:19:13 +00008# executed from the Tix demo program "tixwidgets.py": it must have a
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +00009# procedure called "RunSample". It should also have the "if" statment
10# at the end of this file so that it can be run as a standalone
11# program using tixwish.
12
13# This file demonstrates the use of the tixPopupMenu widget.
14#
Benjamin Petersond6d63f52009-01-04 18:53:28 +000015import tkinter.tix
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000016
17def RunSample(w):
18 # We create the frame and the button, then we'll bind the PopupMenu
19 # to both widgets. The result is, when you press the right mouse
20 # button over $w.top or $w.top.but, the PopupMenu will come up.
21 #
Benjamin Petersond6d63f52009-01-04 18:53:28 +000022 top = tkinter.tix.Frame(w, relief=tkinter.tix.RAISED, bd=1)
23 but = tkinter.tix.Button(top, text='Press the right mouse button over this button or its surrounding area')
24 but.pack(expand=1, fill=tkinter.tix.BOTH, padx=50, pady=50)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000025
Benjamin Petersond6d63f52009-01-04 18:53:28 +000026 p = tkinter.tix.PopupMenu(top, title='Popup Test')
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000027 p.bind_widget(top)
28 p.bind_widget(but)
29
Tim Peters182b5ac2004-07-18 06:16:08 +000030 # Set the entries inside the PopupMenu widget.
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000031 # [Hint] You have to manipulate the "menu" subwidget.
Tim Peters182b5ac2004-07-18 06:16:08 +000032 # $w.top.p itself is NOT a menu widget.
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000033 # [Hint] Watch carefully how the sub-menu is created
34 #
35 p.menu.add_command(label='Desktop', underline=0)
36 p.menu.add_command(label='Select', underline=0)
37 p.menu.add_command(label='Find', underline=0)
38 p.menu.add_command(label='System', underline=1)
39 p.menu.add_command(label='Help', underline=0)
Benjamin Petersond6d63f52009-01-04 18:53:28 +000040 m1 = tkinter.tix.Menu(p.menu)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000041 m1.add_command(label='Hello')
42 p.menu.add_cascade(label='More', menu=m1)
43
Benjamin Petersond6d63f52009-01-04 18:53:28 +000044 but.pack(side=tkinter.tix.TOP, padx=40, pady=50)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000045
Benjamin Petersond6d63f52009-01-04 18:53:28 +000046 box = tkinter.tix.ButtonBox(w, orientation=tkinter.tix.HORIZONTAL)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000047 box.add('ok', text='Ok', underline=0, width=6,
Tim Peters182b5ac2004-07-18 06:16:08 +000048 command=lambda w=w: w.destroy())
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000049 box.add('cancel', text='Cancel', underline=0, width=6,
Tim Peters182b5ac2004-07-18 06:16:08 +000050 command=lambda w=w: w.destroy())
Benjamin Petersond6d63f52009-01-04 18:53:28 +000051 box.pack(side=tkinter.tix.BOTTOM, fill=tkinter.tix.X)
52 top.pack(side=tkinter.tix.TOP, fill=tkinter.tix.BOTH, expand=1)
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000053
54if __name__ == '__main__':
Benjamin Petersond6d63f52009-01-04 18:53:28 +000055 root = tkinter.tix.Tk()
Martin v. Löwisb21cb5f2001-03-21 07:42:07 +000056 RunSample(root)
57 root.mainloop()