button1的=按鈕(根,文本=「還原圖像」,前景=「紅」,化合物=「中心」)的Python 2.7 Tkinter的如何改變按鈕的文本的文字顏色
這種類型的代碼是不加工。它說未知選項「-foreground」。
這是工作的整個代碼 -
from Tkinter import *
from ttk import *
def change():
label.config(text="Hey dude !!")
label.config(image = img1,background='blue',foreground='yellow')
def click():
if button1.instate(["disabled"]):
label.config(image = img1,background='yellow',foreground='green')
button1.state(['!disabled'])
button.state(['disabled'])
else:
label.config(image = img,background='yellow',foreground='green')
button1.state(['disabled'])
button.state(['!disabled'])
root = Tk()
label = Label(root)
img=PhotoImage(file='C:\\Users\\Vivek\\Desktop\\x.gif')
img1= PhotoImage(file='C:\\Users\\Vivek\\Desktop\\y.gif')
img2 = PhotoImage(file='C:\\Users\\Vivek\\Desktop\\z.gif')
button = Button(root)
button.pack()
button1 = Button(root,text='Revert image',compound="center")
img2_small = img2.subsample(30,80)
button.config(image=img2_small,text='Change image',compound='center')
button1.state(["disabled"])
button1.pack()
label.pack()
button.config(command=click)
button1.config(command = click)
label.config(image = img,background='yellow',foreground='green')
label.config(text = 'Hey dude watsup ?? Are you in a need help ?')
label.config(compound = 'left',wraplength=100,font=('Courier',20,'bold'))
label.after(5000,change)
root.mainloop()
仍然沒有運氣。它說 - NameError:名稱'tk'未定義。 –
一切正常,如果我不嘗試使用fg/foreground更改顏色。 –
在上面的例子中,我將tkinter導入爲tk,這是使用tkinter等庫時的標準做法。不要冒犯,但我會假設你對tkinter有點新鮮。 Button不在tkinter之外,所以你發佈的代碼片段將不起作用,除非你將庫中的所有內容都導入你的名字空間,這是不好的做法。考慮粘貼一個你沒有指定顏色的代碼的修剪下來的例子。 – BlivetWidget