File: cbf_autoscrolled_window.py

package info (click to toggle)
cbflib 0.9.7%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 65,272 kB
  • sloc: ansic: 131,361; python: 22,780; sh: 3,108; makefile: 2,088; yacc: 659; java: 223; f90: 214; xml: 210; cpp: 58
file content (174 lines) | stat: -rw-r--r-- 7,334 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# cbf_autoscrolled_window.py
# utilities to create windows with a both a vertical
# scroll bar and a horizontal scroll bar with each scroll bar
# disappearing when the window is large enough
#
# copyright (c) 2011 Herbert J. Bernstein
# portions from
#    http://effbot.org/zone/tkinter-autoscrollbar.htm
#  Autohiding Scrollbars
#  August 08, 1998 | Fredrik Lundh
#
#
# for incorporation into the CBFlib package API
#
######################################################################
#                                                                    #
# YOU MAY REDISTRIBUTE THE CBFLIB PACKAGE UNDER THE TERMS OF THE GPL #
#                                                                    #
# ALTERNATIVELY YOU MAY REDISTRIBUTE THE CBFLIB API UNDER THE TERMS  #
# OF THE LGPL                                                        #
#                                                                    #
######################################################################

########################### GPL NOTICES ##############################
#                                                                    #
# This program is free software; you can redistribute it and/or      #
# modify it under the terms of the GNU General Public License as     #
# published by the Free Software Foundation; either version 2 of     #
# (the License, or (at your option) any later version.               #
#                                                                    #
# This program is distributed in the hope that it will be useful,    #
# but WITHOUT ANY WARRANTY; without even the implied warranty of     #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
# GNU General Public License for more details.                       #
#                                                                    #
# You should have received a copy of the GNU General Public License  #
# along with this program; if not, write to the Free Software        #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA           #
# 02111-1307  USA                                                    #
#                                                                    #
######################################################################

######################### LGPL NOTICES ###############################
#                                                                    #
# This library is free software; you can redistribute it and/or      #
# modify it under the terms of the GNU Lesser General Public         #
# License as published by the Free Software Foundation; either       #
# version 2.1 of the License, or (at your option) any later version. #
#                                                                    #
# This library is distributed in the hope that it will be useful,    #
# but WITHOUT ANY WARRANTY; without even the implied warranty of     #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  #
# Lesser General Public License for more details.                    #
#                                                                    #
# You should have received a copy of the GNU Lesser General Public   #
# License along with this library; if not, write to the Free         #
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,    #
# MA  02110-1301  USA                                                #
#                                                                    #
######################################################################



import sys
try:
  import Tkinter as TKINTER
  import tkFileDialog as TKFILEDIALOG
except ImportError:
  try:
    import tkinter as TKINTER
    import tkinter.filedialog as TKFILEDIALOG
  except ImportError:
    print("CBF: cbf_autoscrolled_window requires Tkinter")
    sys.exit(1)





#################################################################
#  The following autoscrollbar logic from               
#    http://effbot.org/zone/tkinter-autoscrollbar.htm
#  Autohiding Scrollbars
#  August 08, 1998 | Fredrik Lundh
#
class AutoScrollbar(TKINTER.Scrollbar):
    # a scrollbar that hides itself if it's not needed.  only
    # works if you use the grid geometry manager.

    __disabled = False
    
    def set(self, lo, hi):
        if self.__disabled or (float(lo) <= 0.0 and float(hi) >= 1.0):
            # grid_remove is currently missing from Tkinter!
            self.tk.call("grid", "remove", self)
        else:
            self.grid()
        TKINTER.Scrollbar.set(self, lo, hi)
    def pack(self, **kw):
        raise TclError, "cannot use pack with this widget"
    def place(self, **kw):
        raise TclError, "cannot use place with this widget"
#################################################################
    def disable(self):
        self.__disabled = True
        self.tk.call("grid", "remove", self)
    def enable(self):
        self.__disabled = False
        self.grid()

class cbf_autoscrolled_window(TKINTER.Frame):
    """ Class cbf_autoscrolled window returns an object
        giving a frame and a canvas that are have visible
        scroll bars when needed.

        Use:
          window=cbf_autoscrolled_window(rootwin)
          ... build scene in window.frame
          window.frame.update_idletasks()
          window.canvas.config(scrollregion=window.canvas.bbox(TKINTER.ALL))

          rootwin.mainloop()

        The frame is accessible as window.frame
        The canvas is accessible as window.canvas
    """

          
  
    def __cbf_create_autoscrolled_window(self,rootwin):
        rootwin.grid_columnconfigure(0,weight=1)
        rootwin.grid_columnconfigure(1,weight=0,minsize=8,pad=0)
        rootwin.grid_rowconfigure(0,weight=1)
        rootwin.grid_rowconfigure(1,weight=0,minsize=8,pad=0)
        self.__hsb = AutoScrollbar(rootwin,orient=TKINTER.HORIZONTAL)
        self.__hsb.grid(row=1,column=0, sticky="news")
        self.__vsb = AutoScrollbar(rootwin)
        self.__vsb.grid(row=0,column=1, sticky="news")
        self.canvas = TKINTER.Canvas(rootwin, xscrollcommand=self.__hsb.set, \
                            yscrollcommand=self.__vsb.set)
        self.__vsb.config(command=self.canvas.yview)
        self.__hsb.config(command=self.canvas.xview)
        self.canvas.grid(row=0,column=0,sticky="news")    
        self.frame = TKINTER.Frame(self.canvas)
        self.canvas.create_window(0, 0, anchor=TKINTER.NW, window=self.frame)
        self.frame.update_idletasks()
        self.canvas.config(scrollregion=self.canvas.bbox(TKINTER.ALL))
        self.frame.rowconfigure(1, weight=0, pad=0, minsize=10)
        self.frame.rowconfigure(0, weight=1, pad=0, minsize=8)
        self.frame.columnconfigure(1, weight=0, pad=0, minsize=10)
        self.frame.columnconfigure(0, weight=1, pad=0, minsize=8)


    def __init__(self,rootwin):
        self.root=rootwin
        self.canvas=None
        self.__hsb=None
        self.__vsb=None
        self.frame=None
        self.__cbf_create_autoscrolled_window(rootwin)

    def horizontal_scroll_disable(self):
        self.__hsb.disable()

    def horizontal_scroll_enable(self):
        self.__hsb.enable()
        
    def vertical_scroll_disable(self):
        self.__vsb.disable()
        
    def vertical_scroll_enable(self):
        self.__vsb.disable()