Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tkinter Mainloop Greyed Out In VS Code

I made wordle, but I want to make it look more like the official wordle so my goal is to make 5 tkinter entries on every line, and have them feed into 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 :
        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)
entry2.grid(row=2, column=0)
entry3 = Entry(root, width = 22,font = ('Georgia 18'), justify=CENTER)
entry3.grid(row=4, column=0)

while True:
    testlen()

        
root.mainloop()

What did I do wrong?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading