2015-10-04 130 views
0

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

回答

-1

我用FG

button1 = tk.Button(root, text='hello', fg='red') 

編輯:HM,其實,對我來說都fg和​​工作。如果你不喜歡這種顏色,那麼其他一切工作嗎?這可能是一些其他錯誤傳播下來。這是一個簡單的使用tkinter的Hello World程序的例子。看看它是否適合你。我想的Tkinter的市值已經Python 2和3之間變化這是Python 3的

import tkinter as tk 

class Application(tk.Frame): 
    def __init__(self, master=None): 
     super().__init__(master) 
     self.master = master 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 
     self.TVB1 = tk.StringVar(self, value='hello, there') 

     B1 = tk.Button(self) 
     # this is an example of how you can define parameters after 
     # defining the button 
     B1["textvariable"] = self.TVB1 
     B1["command"] = self.say_hi 
     B1.grid(row=0,column=0) 

     self.TVE1 = tk.StringVar(self, value='wubwub') 
     E1 = tk.Entry(self, textvariable=self.TVE1) 
     E1.grid(row=1, column=0) 

     # and this is how you can define parameters while defining 
     # the button 
     Quit = tk.Button(self, text='QUIT', fg='red', 
           command=self.master.destroy) 
     Quit.grid(row=2,column=0) 

    def say_hi(self): 
     print(self.TVB1.get()) 
     self.TVB1.set(self.TVE1.get()) 


root = tk.Tk() 
app = Application(root) 
app.mainloop() 
+0

仍然沒有運氣。它說 - NameError:名稱'tk'未定義。 –

+0

一切正常,如果我不嘗試使用fg/foreground更改顏色。 –

+0

在上面的例子中,我將tkinter導入爲tk,這是使用tkinter等庫時的標準做法。不要冒犯,但我會假設你對tkinter有點新鮮。 Button不在tkinter之外,所以你發佈的代碼片段將不起作用,除非你將庫中的所有內容都導入你的名字空間,這是不好的做法。考慮粘貼一個你沒有指定顏色的代碼的修剪下來的例子。 – BlivetWidget

-1

試試這個在Python 2.7:

進口Tkinter的傳統知識

這樣的Tkinter與資本Ť

+0

Python 2.7沒有名爲'tkinter'的模塊,它是Python 2中的'Tkinter' –

+0

,tkinter是Tkinter。您輸入的內容無關緊要。要遵循標準格式,您可以使用「導入Tkinter作爲tk」 – BlivetWidget

1

因爲您正在進行全球進口(很少有一個好主意),並且因爲您在tkinter之後導入ttk。這兩個庫定義了一個小工具,所以ttk Button覆蓋了tkinter Button。 ttk Button沒有​​選項。

應停止使用全球進口來解決這個問題:

import Tkinter as tk 
import ttk 
... 
root = tk.Tk() 
... 
tk.Button(...) 
+0

謝謝:) 但爲什麼發生這種問題?所以我導入了整個庫,這意味着我的代碼效率不好,但問題背後的原因是什麼? –

+0

@Vivekkumarpathak:問題背後的原因是兩個庫都定義了'Button',但它們是不同的。一旦支持「前景」,一個不會。 –

0

所以考慮看看右here你可以看到,「前景」和「FG」選項是一樣的。但是在python3的tkinter新版本中只有這種情況,如果你使用python2.7的舊版本,你必須使用「fg」選項。

btn = Button(root, fg='red') #Creates a button with red text 

如果您想更改文本顏色之後,您可以通過使用配置功能實現這一點:

btn.config(fg='blue') #Changes the text color to blue 

我希望這清除了一點東西。 保持編碼; D

相關問題