Python Forum

Full Version: text widgets-insert problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am tring to pick 5 digits randomly( one line in one second) and display them om tkinter gui using either Label or Text widgets.

it works fine on python-idle(console) but ı need to know how to put them on Text widgets Huh


from tkinter import*
import random
import time
wn=Tk()
wn.title("myfirst-gui")
wn.geometry("300x400")
wn.configure(bg="pink")

for i in range(5):    
    mydigits=random.sample(range(1,30),5)
    print(mydigits)
    time.sleep(1)
    lb1=Label(bg="silver",text=mydigits,width=10)
    lb1.place(x=100,y=100)
    """label widgets displays only the last line so  I tried the text widget but it doesnt work
    """
    text_widget=Text(bg="green",fg="white")
    text_widget.insert('1.0', mydigits)
    text_widget.place(x=300,y=300)


wn.mainloop()
I think I should modify some part of mycode to make it clear...
from tkinter import*
import random
import time
wn=Tk()
wn.geometry("300x300")
 
for i in range(5):
    loto=random.sample(range(1,30),6)
    mytext=Text(wn,bg="pink",width=30,height=200)
    mytext.pack()
    mytext.insert('1.0', loto,"\n")
    mytext = mytext.get('1.0', 'end')
    time.sleep(1)




wn.mainloop()
I still cant make it work Snooty Dodgy
I think what you're asking is how to run from command line. Correct?
from tkinter import*
import random
import time

def main_process():
    for i in range(5):    
        mydigits=random.sample(range(1,30),5)
        print(mydigits)
        time.sleep(1)
        lb1=Label(bg="silver",text=mydigits,width=10)
        lb1.place(x=100,y=100)
        """label widgets displays only the last line so  I tried the text widget but it doesnt work
        """
        text_widget=Text(bg="green",fg="white")
        text_widget.insert('1.0', mydigits)
        text_widget.place(x=300,y=300)

def main():
    wn=Tk()
    wn.title("myfirst-gui")
    wn.geometry("300x400")
    wn.configure(bg="pink")
    main_process()
    wn.mainloop()

if __name__ == '__main__':
    main()
to run from command line:
python myprogram.py
replacing myprogram with actual filename
You cant use time.sleep in gui code it blocks the mainloop.
see my thread How to deal with code that blocks the mainloop freezing the gui