I am writing a program with python and Tkinter.  I have encountered a
mysterious problem with the whole application hanging itself (cannot be
stopped with ctrl-c, must be killed from another console).  I have done
several tests with interactive mode, and here what I noticed:
- the problem is with a class MandelSetWindow that I attach, as other
classes work properly

- the class can be initialized properly, but behaves strangely with
other windows.  Here is how to reproduce this bug:

* import Tkinter.Tk and this class
* create a Tk() instance and take a reference to it
* create an instance to this class (can use (None, None) as arguments)
and take a reference to it

Now everything looks normal.  Both references refer to objects like they
were created.  But try to e.g. withdraw() either the root Tk window or
this dialog.  You will notice that the other withdraws.  I really can't
understand why it happens like this.

zefciu
import Tkinter
from mandelpalette import MandelPalette

class MandelSetWindow(Tkinter.Toplevel, object):
    """A dialog window with main fractal settings"""
    def __init__(self, settings, callback):
        Tkinter.Toplevel.__init__(self)
        self.callback = callback
        self.textfields = {}
        row = 0
        for group in [("size", ["width", "height"], "pixel"),
                ("scale", ["XMin", "XMax", "YMin", "YMax"], ""),
                ("fractal parameters", ["bailout radius"], "")]:
            Tkinter.Label(text = group[0]).grid(row = row, column = 0,
                    columnspan = 3)
            row += 1
            for param in group[1]:
                Tkinter.Label(text = param).grid(row = row, column = 0)
                self.textfields[param] = Tkinter.Entry(width = 10)
                self.textfields[param].grid(row = row, column = 1)
                Tkinter.Label(text = group[2]).grid(row = row, column = 2)
                row += 1
        Tkinter.Label(text="palette type").grid(row = row, column = 0)
        self.palettebox = Tkinter.Listbox(height = 3)
        for paltype in MandelPalette.palette_types:
            self.palettebox.insert(Tkinter.END, paltype)
        self.palettebox.grid(row = row, column = 1)
        row += 1
        Tkinter.Label(text="Iteration number").grid(row = row, column = 0)
        self.textfields["iterations"] = Tkinter.Entry(width = 10)
        self.textfields["iterations"].grid(row = row, column = 1)
        row += 1
        for butt in [("OK", self.__submit, 0), ("Cancel", self.__cancel, 1), 
                ("Reset", self.__reset, 2)]:
            Tkinter.Button(text = butt[0], command = butt[1]).grid(row = row,
                    column = butt[2])
        row += 1

    def __getsettings(self):
        result = {}
        params = ["width", "height", "XMin", "XMax", "YMin", "YMax"]
        for i in [("size", 2), ("scale", 4)]:
            result[i[0]] = tuple([float(self.textfields[params.pop(0)].\
                    get()) for j in range(0, i[1])]) 
        result["bailout"] = float(self.textfields["bailout radius"].get())
        paltype = self.palettebox.get(self.palettebox.curselection())
        iternumber = int(self.textfields["iterations"].get())
        result["palette"] = mandelpalette.MandelPalette(iternumber, paltype, 1)
        return result

    settings = property(__getsettings)

    def __submit(self):
        self.callback(self.settings)


    def __cancel(self):
        pass
    def __reset(self):
        pass
    def __fill(self, settings):
        pass
class MandelPalette(list):
    def __init__(self, size, type, cycles = 1):
        try:
            self.palette_types[type](self, size, cycles)
        except KeyError:
            raise ValueError("This palette type is not implemented")
    
    def __genegray(self, size, cycles):
        size += 1 # compensating for palette[0]
        (levpercycle, remainder) = divmod(size, cycles)
        for i in range(0, cycles):
            if remainder: # remainder is compensated by first several cycles
                remainder -= 1
                levpercurrent = levpercycle + 1
            else:
                levpercurrent = levpercycle
            step = 255.0 / (levpercurrent - 1) # if we don't add this -1 
                                             # we won't get white 
            for j in range(0, levpercurrent):
                level = int(j * step)
                self.append((level, level, level))

    def __generain(self, size, cycles):
        pass

    def __generand(self, size, cycles):
        pass

    palette_types = {"grayscale" : __genegray,
            "rainbow" : __generain,
            "random" : __generand}
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to