#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11

    Syntax Highlighting is freezing???


    hi

    Why does this code after i press the button freeze???

    Code:
    def Find(self):
            Top = Toplevel(self.root)
            Tofind = StringVar()
            Entry(Top,textvariable=Tofind).pack(side=TOP)
            def DO():
                while True:
                    idx = self.txt.search(Tofind.get(),"1.0",nocase=1,stopindex=END)
                    if not idx: break
                    lastidx = "%s+%dc" % (idx,len(SEL))
                    self.txt.tag_add("Found",idx,lastidx)
                    idx = lastidx
                    self.txt.tag_config("Found",background="yellow") 
            Button(Top,text="Find",command=DO).pack(side=BOTTOM)            #  #
    and there is no error message displayed...
    Those people who think they know everything are a great annoyance to those of us who do.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    This is probably happening because you're using a while True loop, since the loop never exits Python can't update the GUI so it freezes. The same is true you put a while True loop that never exits into any program (none theaded). Just for fun you can try this in the Python shell .

    Code:
    >>> while True: print 'loop'
    ...
    Hope this helps,

    Mark.
    Last edited by netytan; July 6th, 2005 at 06:22 PM. Reason: Typo
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    netytan is right, you have an endless loop.
    Code:
    idx = self.txt.search(Tofind.get(),"1.0",nocase=1,stopindex=END)
    You're searching always between "1.0" and END. If the word you are searching is in the line, your code always finds the same one (the first), so your if not idx: break is never true and the loop is endless
    Try this instead:
    Code:
    def do():
        start = "1.0"
        while True:
            idx = txt.search(Tofind.get(),start,nocase=1,stopindex=END)
            if not idx: break
            lastidx = "%s+%dc" % (idx, len(SEL))
            txt.tag_add("Found",idx,lastidx)
            start = lastidx
            txt.tag_config("Found",background="yellow")
    Regards, mawe
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    so u mean I should remove the loop and then it will work

    but it is a part of a big program that has to keep on searching for the whole time...

    It would be used in a thread but that does not work either

    I thought about triggering it every 10 seconds or something ....
    Those people who think they know everything are a great annoyance to those of us who do.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Originally Posted by monkeyman23555
    so u mean I should remove the loop and then it will work
    You didn't read my reply, hm?
    The problem is not the loop itself, but you always search the same part of the line, and so you always find the same word. That doesn't make much sense, and that's what causes the endless loop
    Originally Posted by monkeyman23555
    but it is a part of a big program that has to keep on searching for the whole time...
    Really the whole time? I guess you want to make an editor with syntax-highlighting, right? So, bind def Do() to <Any-Key>, and let it only search the part of the text which is new. The rest is already highlighted.

    Regards, mawe
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    true

    I i did not really look at your reply sorry

    ya I do want to make a editor...

    thanks
    Those people who think they know everything are a great annoyance to those of us who do.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    well I made a new one and it highlights in between two parts of the text...

    Code:
        def Highlight(self):
            for KEY in self.Keywords:
                start = "1.0"
                while True:
                    idx = self.txt.search(KEY,start,nocase=1,stopindex=END)
                    if not idx: break
                    lastidx = "%s+%dc" % (idx, len(KEY))
                    self.txt.tag_add("KEYWORD",idx,lastidx)
                    start = lastidx
                    self.txt.tag_config("KEYWORD",foreground="blue")
    
            start = "1.0"
            while True:
                stridx = self.txt.search('"',start,nocase=1,stopindex=END)
                if not stridx: break
                strend = self.txt.search('"',stridx,nocase=1,stopindex=END)
                if not strend: break
                self.txt.tag_add("STRING",stridx,strend)
                start = strend
                self.txt.tag_config("STRING",foreground="yellow")
    I am taking about the bottom part I don't really understand why it always freezes...
    Those people who think they know everything are a great annoyance to those of us who do.
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Very simply, a program can only do one thing at a time so if you spend all your time in an infinite loop the computer can't update the GUI. For this reason GUI applications are usually multi-threaded: one thead handles redrawing the GUI and taking user input, the other thread handles the actual work of the program .

    That said you should be able to do without another thread if you can get the loop to exit. I'm not familar with TK but it seems logical to me that 'start' should be an int not a str, assuming of course that it's an index like I suspect.

    You should also check to see what self.txt.Search() is returning to make sure that it will be evaluated to False if the string isn't found.

    In either case the second loop seems more promising since the code should actually move though the string; unlike the first. Just a thought but why not use the in operator to check if the substring exists?

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    I am not sure if you are understanding I need help with the second part of the highlighting function which is from the second start and yes I have a thread to handle highlighting() and then the main thread to hndle the gui and it works but it always crashes when I try highlighting() with a menu command which is at the moment instead of the thread.
    Those people who think they know everything are a great annoyance to those of us who do.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Did you have a look at the link I posted? There you'll find an editor with syntax highlighting in Tkinter. It's not perfect, but you may find some hints how to implement yours.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    okay thanks
    Those people who think they know everything are a great annoyance to those of us who do.

IMN logo majestic logo threadwatch logo seochat tools logo