亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Tkinter 文本框(隨機/交替改變 Tkinter 文本框的前景色)

Tkinter 文本框(隨機/交替改變 Tkinter 文本框的前景色)

四季花海 2023-03-30 10:40:58
#Text FrameTextFrame = Frame(win, bg="#003030")TextFrame.pack(fill=BOTH, expand=True)#Create a Text BoxTextBox = Text(TextFrame)TextBox.pack(side="bottom", fill=BOTH, expand=True)#Color Pickerchoose = ["#ff0000", "#0000ff"]color = choice(choose)#Button Functiondef 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 顏色。
查看完整描述

2 回答

?
呼如林

TA貢獻1798條經驗 獲得超3個贊

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

終于設法通過使用 tag_configure 獲得了想要的結果。不是隨機單詞,而是字母,我可以接受。


查看完整回答
反對 回復 2023-03-30
?
當年話下

TA貢獻1890條經驗 獲得超9個贊

首先,您必須創建具有初始前景色的文本小部件。然后你需要在一個定時的事情中改變前景的顏色。您可以使用after()該方法:


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


查看完整回答
反對 回復 2023-03-30
  • 2 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號