Python Forum
[Tkinter] text widgets-insert problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] text widgets-insert problem
#1
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()
Reply
#2
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
Reply
#3
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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] text get index problem philipbergwerf 2 1,721 Feb-03-2022, 01:17 AM
Last Post: philipbergwerf
  tkinter| listbox.insert problem Maryan 3 3,440 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  how to insert image into Text widget Tkinter atlass218 5 9,941 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] Problem with changing label text on button press xk2006x 1 5,548 Jun-02-2017, 06:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020