Bug in Tkinter.ScrolledText (and fix).

Peter Haight peterh at sapros.com
Wed Sep 15 15:25:01 EDT 1999


If you create multiple ScrolledText widgets it uses the same name for all
of them. This is because the line cnf['name'] = 'text' clobbers the default
value for cnf which was created inside class scope. This means the next time
__init__ is called with no cnf argument, cnf now has the default value of {
'name' : 'text' } instead of {}.

I'm including some sample code and a patch.

----------- PATCH ----------------
--- ScrolledText.py.orig	Wed Sep 15 12:09:54 1999
+++ ScrolledText.py	Wed Sep 15 12:15:22 1999
@@ -14,7 +14,9 @@
 from Tkinter import _cnfmerge
 
 class ScrolledText(Text):
-	def __init__(self, master=None, cnf={}, **kw):
+	def __init__(self, master=None, cnf=None, **kw):
+		if cnf is None:
+			cnf = {}
 		if kw:
 			cnf = _cnfmerge((cnf, kw))
 		fcnf = {}
-----------------------------------

-------- SAMPLE CODE --------------

#!/usr/bin/env python

from Tkinter import *
import ScrolledText

class App(Frame):
	def __init__(self, master=None):
		Frame.__init__(self, master)
		self.pack(expand=1, fill=BOTH)
		self.texts = []
		self._create_widgets()

	def _create_widgets(self):
		self.entry = Entry(self)
		self.entry.pack(side=TOP, expand=0, fill=X)
		self.entry.bind('<Return>', self.change)
		self.entry.focus_set()

		self.pane = Frame(self)
		self.pane.pack(side=TOP)
		for i in range(6):
			t = ScrolledText.ScrolledText(self)
			#t = Text(self)
			print 'Created text:', t
			t.insert('end', 'Screen %i' % i)
			self.texts.append(t)

		self.curscreen = 0
		self.texts[0].pack(side=TOP, expand=1, fill=BOTH)
	
	def change(self, event):
		try:
			num = int(self.entry.get())
		except ValueError:
			return
		if num == self.curscreen: return
		if num >= 6: return
		self.texts[self.curscreen].forget()
		self.texts[num].pack(side=TOP, expand=1, fill=BOTH)
		self.curscreen = num

def main():
	a = App()
	a.mainloop()

if __name__ == '__main__':
	main()
-----------------------------------





More information about the Python-list mailing list