0

I've got the problem that I want to call some code after the mainloop of Python which is written in a secound class. But this Class contains a possible endless loop which will cause Tkinter to freeze when I try to call the mainloop() after it. Is it possible to get the data from the first class to the Window even though it may could be endless? here is my current code:

from tkinter import *
import re
import threading
import _thread as thread
from time import sleep

class interpret(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        print("some text")
        self.read()   
    def read(self):
        laenge = self.file_len(self.file)
        try:
            while self.line < laenge and not self.graceful:
                fp = open(self.file)
                for i, line in enumerate(fp):
                    if i == self.line:
                        content = line.rstrip().lower()
                        content = content.split(";", 1)[0]
                        content = self.replace(content)
                        #simply is getting some Info of the File, can change self.line
                        #it also could trigger an input(), which I want to replace by the canvas input
                        self.get_operator(content)
                self.line += 1
                fp.close()
        except KeyboardInterrupt:
            print("\n\n-----Info-----")
            for i in range(len(self.memory)):
                print(i,":",self.memory[i])
        except IndexError:
            print("Buffer Overflow in line",self.line+1)
class Window(threading.Thread):
    pointer = 40
    h= 500
    txt = []
    master = Tk()
    buffer_txt = []
    expect_input = True
    input_method = 1 #0=Nothing,1=Alphanumeric,2=Everything

    def __init__(self):
        threading.Thread.__init__(self)
        self.w = Canvas(self.master, width=500, height=self.h, background="black",highlightthickness=0)
        self.w.pack()
        self.master.bind("<Return>", self.send)
        self.master.bind("<Key>", self.to_buffer)
        self.w.update()
        mainloop() 
    def update(self):
        if self.txt != []:
            for i in range(len(self.txt)):
                self.w.coords(self.txt[i], 20, ((i*15)+30))
    def text_add(self,text):
        if(len(self.txt) > 30):
            self.txt = self.txt[1:]
            self.w.delete(self.txt[0])
            self.update()
        self.txt += [self.w.create_text(20, self.pointer, text=text, fill="white",anchor="w")]
        if self.pointer < 485:
            self.pointer += 15
        self.w.update()
    def to_buffer(self,event):
        if self.expect_input:
            if (((event.keycode >= 48 and event.keycode <= 57) or (event.keycode >= 65 and event.keycode <= 90) or (event.keycode >= 97 and event.keycode <= 122) or event.keycode == 8 or event.keycode == 32) and self.input_method == 1) or self.input_method == 2:
                print("My Mode:",self.input_method,"My keycode:",event.keycode)
                self.w.delete(self.txt[len(self.txt)-1])
                self.txt = self.txt[:len(self.txt)-1]
                print("Out:",str(event.keycode))
                s = str(event.char)
                if event.keycode == 8 and len(self.buffer_txt) > 0:
                    del self.buffer_txt[-1]
                else:
                    self.buffer_txt += [s]
                t = "".join(self.buffer_txt)
                self.txt += [self.w.create_text(20, self.pointer, text=t, fill="white",anchor="w")]
                self.update()
            else:
                print("My Keycode:",event.keycode)
    #text_add(repr(event.char))
    def send(self,none):
        t = "".join(self.buffer_txt)
        self.buffer_txt = []
        print("text:",t)
        self.text_add(t)
        #This is the variable I want to send to the interpret Class as an input() alternative
        self.text = t
        self.w.delete(self.txt[-1])

if __name__ == '__main__':
    thread1 = Window()
    thread2 = interpret()

    thread1.start()
    thread2.start()

    thread1.join()
    thread2.join()

What I try to do is that the Window should be an input alternative in a bit more flexible way. I cut out some unnecessary Code, I hope it is still good to understand what I try to do.

Buffer Overflow
  • 946
  • 1
  • 8
  • 9

1 Answers1

0

The best advice I can give is to not have an infinite loop in your code. You already have an infinite loop, named mainloop. Use that to your advantage. Write a function that performs just one iteration of what you want to do in the loop. Then, after that function is finished, have it check to see if there is more work to be done. if there is, have it use after to call itself again in a few milliseconds. Usually that is sufficiently performant that you won't need to use any threads at all.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685