0

So i have this piece of code where whenever you press a button it triggers a .bind() event that is supposed to call a method to update Text box contents but it is late by 1 loop e.g. i press the "a" character and then press "b" it will only refresh and show "a" on screen right after i pressed "b", however, i also log the key that is pressed which shows it as "b" was pressed yet still wasn't refreshed.

here's a snippet of my code

from tkinter import *

class main():
    def __init__(self):
        root = self.root = Tk()

        kazalphabet = {"А":"A","Ә":"Ä","Б":"B","В":"V","Г":"G","Ғ":"Ğ","Д":"D","Е":"E","Ё":"YO","Ж":"J","З":"Z","И":"Ï","Й":"Y","К":"K","Қ":"Q","Л":"L","М":"M","Н":"N","Ң":"Ñ","О":"O","Ө":"Ö","П":"P","Р":"R","С":"S","Т":"T","У":"W","Ұ":"U","Ү":"Ü","Ф":"F","Х":"X","Һ":"H","Ц":"C","Ч":"Ç","Ш":"Ş","Щ":"ŞÇ","Ъ":"\"","Ы":"I","І":"I","Ь":"\'","Э":"É","Ю":"YU","Я":"YA","а":"a","ә":"ä","б":"b","в":"v","г":"g","ғ":"ğ","д":"d","е":"e","ё":"yo","ж":"j","з":"z","и":"ï","й":"y","к":"k","қ":"q","л":"l","м":"m","н":"n","ң":"ñ","о":"o","ө":"ö","п":"p","р":"r","с":"s","т":"t","у":"w","ұ":"u","ү":"ü","ф":"f","х":"x","һ":"h","ц":"c","ч":"ç","ш":"ş","щ":"şç","ъ":"\"","ы":"ı","і":"i","ь":"\'","э":"é","ю":"yu","я":"ya"}
        #dictionary used to transliterate

        def realtimeChanger(stringinp,alphabetDic): #magic code, works, dont touch
            procingstring = stringinp
            for i in alphabetDic:
              x = alphabetDic.get(i)
              procingstring = procingstring.replace(i,x)
            return procingstring

        global txtboxbuffer #global var used in local GUI.py and other files

        txtbox = Text(root, height = 20, width = 50)
        txtbox.grid(row=2, column=0, pady = 5, padx = 5)
        outbox = Text(root, height = 20, width = 50)
        outbox.grid(row=2, column=1, pady = 5, padx = 5)

        def updateout(event): # func to update outbox contents and processs txtbox
            print("pressed", event.char)
            txtboxbuffer = realtimeChanger(txtbox.get("1.0", "end"), kazalphabet)
            outbox.delete("1.0", "end")  # if you want to remove the old data
            outbox.insert(END,txtboxbuffer)
            print("txtboxbuffer contents are:", txtboxbuffer)

        txtbox.bind("<Key>", updateout)# bind all keys to updateout() func
main()
Runley
  • 23
  • 5
  • It is entirely intentional that a key binding executes *before* the key is actually inserted in the Text - this allows you to filter out keys that you don't want to be inserted, for example. One solution would be to have the binding instead execute a function that does `root.after(1, updateout)`, so that your code executes slightly later, after the insertion has taken place. – jasonharper Mar 16 '20 at 16:27
  • Use `''` instead. – stovfl Mar 16 '20 at 17:06

0 Answers0