KSmith
KSmith

Reputation: 57

Tkinter Mainloop Unreachable

I made wordle, but I want to make it look more like the official wordle so my final goal is to make 5 tkinter entries on every row, and have them linked to each other. When I run this code, mainloop at the bottom is greyed out and no window appears.


root = Tk()
root.geometry('400x400')

def testlen():
    global textinentry1
    textinentry1= entry1.get()
    if len(textinentry1)  >1 :
        entry1.delete(0,END)
        entry1.insert(0,textinentry1[0])
        entry2.delete(0, END)
        entry2.insert(0,textinentry1[1] )

     

entry1 = Entry(root,width=5,  font = ('Georgia 18'), justify=CENTER)
entry1.grid(row=0, column=0)
entry2 = Entry(root, width=5,  font = ('Georgia 18'), justify=CENTER)
entry2.grid(row=0, column=1)
entry3 = Entry(root, width = 5,font = ('Georgia 18'), justify=CENTER)
entry3.grid(row=0, column=2)

while True:
    testlen()

        
root.mainloop()

What did I do wrong?

Upvotes: 1

Views: 198

Answers (1)

Sharim09
Sharim09

Reputation: 6224

The mainloop is executed after while loop end Which is never end this is because you are not seeing the window here.

If you want to run this code then you can use the root.after() method to create a loop.

from tkinter import *
root = Tk()
root.geometry('400x400')

def testlen():
    global textinentry1
    textinentry1= entry1.get()
    if len(textinentry1)  >1 :
        entry2.delete(0, END)
        entry2.insert(0,textinentry1[1] )

     

entry1 = Entry(root,width=22,  font = ('Georgia 18'), justify=CENTER)
entry1.grid(row=0, column=0)
entry2 = Entry(root, width=22,  font = ('Georgia 18'), justify=CENTER)
entry1button = Button(root, text="Enter", command = lambda :testlen())
entry1button.grid(row=1, column=0)
entry2.grid(row=2, column=0)
entry3 = Entry(root, width = 22,font = ('Georgia 18'), justify=CENTER)
entry3.grid(row=4, column=0)
def loop():
    testlen()
    root.after(1,loop) # 1 is 1 millisecond. Here root.after method call the loop function after 1 millisecond without crashing your code.
loop()

root.mainloop()

Upvotes: 1

Related Questions