TransWikia.com

Tkinter Text Box (Randomly/Alternatively Change Foreground Color of text box of Tkinter)

Stack Overflow Asked by Sakar Subedi on December 25, 2021

#Text Frame

TextFrame = Frame(win, bg="#003030")
TextFrame.pack(fill=BOTH, expand=True)

#Create a Text Box

TextBox = Text(TextFrame)
TextBox.pack(side="bottom", fill=BOTH, expand=True)

#Color Picker
choose = ["#ff0000", "#0000ff"]

color = choice(choose)

#Button Function

def render():
    global color
    from tkinter import Text
    input = (TextBox.get("1.0",'end-1c')) #Takes input from the Text Box
    win2 = Toplevel()
    win2.attributes("-fullscreen", True)


    TextFull = Text(win2, font="Ubuntu 14",**fg=choice(choose)**, bg="black")
    TextFull.insert(0.0,input) #Displays Input from first window to second window
    TextFull.pack(fill=BOTH, expand=True)

How do I get random fg color from the choose list alternatively in the text box. Currently I am only getting one fg color at a time.

2 Answers

    #Color Picker
colorlist = ["#ff0000", "#0000ff"]

#Button Function

def render():
    global color
    import random
    from tkinter import Text
    input = (TextBox.get("1.0",'end-1c')) #Takes input from the Text Box
    win2 = Toplevel()
    win2.attributes("-fullscreen", True)

    TextFull = Text(win2,
        font="Ubuntu 14",
        fg="white",
        bg="black")

    for i in range (len(colorlist)):
        TextFull.tag_configure(str(i), foreground=colorlist[i])

    # TextFull.insert(0.0,input) #Displays Input from first window to second window
    TextFull.pack(fill=BOTH, expand=True)

    for i in input:
        tempchoice =random.randint(0, len(colorlist)-1)
        TextFull.insert(END, i, str(tempchoice))
    TextFull.insert(END, 'n')

I finally managed to get the desired result by using tag_configure. Not random words but letters and I am okay with it. Thanks to Melvin Chia for answering the solution of this problem on the Tkinter Facebook Group.

Answered by Sakar Subedi on December 25, 2021

First you must create the Text widget with an initial foreground color. Then you need to change the color of the foreground in a timed matter. You can use the after() method for that:

def render():

    def flash():                                            # <-- flash()
        TextFull.config(fg=choice(["#ff0000", "#0000ff"]))  # <-- update color
        win.after(1000, flash)                              # <-- call flash again after 1 second

    global color
    from tkinter import Text
    input = (TextBox.get("1.0",'end-1c')) #Takes input from the Text Box
    win2 = Toplevel()
    win2.attributes("-fullscreen", True)


    TextFull = Text(win2, font="Ubuntu 14", fg='#0000ff', bg="black")
    TextFull.insert(0.0,input) #Displays Input from first window to second window
    TextFull.pack(fill=BOTH, expand=True)
    flash()                                                   # call flash

Answered by Ronald on December 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP