Home > OS >  tkinter app using lots of processing power
tkinter app using lots of processing power

Time:06-04

I've been making an Enigma Machine simulator (python with tkinter), but for some reason, it absolutely eats up my CPU even though it's not a big program. I've been trying to figure out how to optimize it. I've only had 1 python class in 6th grade with pygame, so this is all stuff I'm not familiar with. Help with optimization would be greatly appreciated! This code will work just by pasting into an IDLE window.

I can't quite figure out how to attach my code in the codeblocks without it breaking, so I'll just put the github link with the source code.

https://github.com/briocherockets/enigma.py

CodePudding user response:

Basically, the way you have written your own mainloop keeps the CPU pegged because there is no pauze in your while-loop.

A quick fix would be to add e.g. time.sleep(0.05) to your loop.

But a better solution would be to rewrite it in a canonical way. The original enigma machine does its "calculation" when you press a key. So it would make sense to link the calculation to a key event.

Use the standard mainloop, and bind a function to update the encryption to the <Key> event.

def handle_key(event):
    try:
        rotor3box.delete("1.0", END)
        rotor3box.insert("1.0", rotorticks[2])
        rotor2box.delete("1.0", END)
        rotor2box.insert("1.0", rotorticks[1])
        rotor1box.delete("1.0", END)
        rotor1box.insert("1.0", rotorticks[0])
        uinput = event.char
    except:
        sys.exit()
    # LETTER COUNT UPDATE DETECTION
    if lettercount != len(uinput):
        if lettercount < len(uinput) and len(uinput) > 1:
            if len(uinput) - lettercount > 1:
                for i in range(1, len(uinput) - lettercount   1):
                    active = alpha.find(uinput[i - 1].upper())   1
                    rotate(1, False)
                    encrypt()
                    outputbox.insert(END, alpha[active - 1])
            else:
                active = alpha.find(uinput[len(uinput) - 2].upper())   1
                rotate(1, False)
                encrypt()
                outputbox.insert(END, alpha[active - 1])
        elif lettercount > len(uinput):
            if lettercount - len(uinput) < 2:
                rotate(1, True)
                uoutput = outputbox.get("1.0", END)
                strip = uoutput[:-2]
                outputbox.delete("1.0", END)
                outputbox.insert("1.0", strip)
            else:
                rotate(lettercount - len(uinput), True)
                uoutput = outputbox.get("1.0", END)
                strip = uoutput[: ((lettercount - len(uinput)) * -1) - 1]
                outputbox.delete("1.0", END)
                outputbox.insert("1.0", strip)
        # UPDATE ROTOR WIRING BOXES
        rot1.delete("1.0", END)
        rot1.insert("1.0", "".join(map(str, rotor11)))
        rot2.delete("1.0", END)
        rot2.insert("1.0", "".join(map(str, rotor21)))
        rot3.delete("1.0", END)
        rot3.insert("1.0", "".join(map(str, rotor31)))
        refl.delete("1.0", END)
        refl.insert("1.0", "".join(map(str, ref1)))
    # REDEFINE LETTERCOUNT
    lettercount = len(uinput)


window.bind("<Key>", handle_key)
window.mainloop()

In this example I've bound it to the root window. You might want to bind it to the input window instead.

  • Related