Tkinter 文本框(随机/交替改变 Tkinter 文本框的前景色)

#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)

如何从文本框中的选择列表中获取随机 fg 颜色。目前我一次只能得到一种 fg 颜色。


四季花海
浏览 122回答 2
2回答

呼如林

    #Color Pickercolorlist = ["#ff0000", "#0000ff"]#Button Functiondef 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')终于设法通过使用 tag_configure 获得了想要的结果。不是随机单词,而是字母,我可以接受。

当年话下

首先,您必须创建具有初始前景色的文本小部件。然后你需要在一个定时的事情中改变前景的颜色。您可以使用after()该方法:def render():&nbsp; &nbsp; def flash():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # <-- flash()&nbsp; &nbsp; &nbsp; &nbsp; TextFull.config(fg=choice(["#ff0000", "#0000ff"]))&nbsp; # <-- update color&nbsp; &nbsp; &nbsp; &nbsp; win.after(1000, flash)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # <-- call flash again after 1 second&nbsp; &nbsp; global color&nbsp; &nbsp; from tkinter import Text&nbsp; &nbsp; input = (TextBox.get("1.0",'end-1c')) #Takes input from the Text Box&nbsp; &nbsp; win2 = Toplevel()&nbsp; &nbsp; win2.attributes("-fullscreen", True)&nbsp; &nbsp; TextFull = Text(win2, font="Ubuntu 14", fg='#0000ff', bg="black")&nbsp; &nbsp; TextFull.insert(0.0,input) #Displays Input from first window to second window&nbsp; &nbsp; TextFull.pack(fill=BOTH, expand=True)&nbsp; &nbsp; flash()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# call flash
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python