簡體   English   中英

Python 2.7 Tkinter如何更改按鈕文本的文本顏色

[英]Python 2.7 Tkinter how to change text color of a button's text

button1 =按鈕(root,text ='Revert image',foreground =“red”,compound =“center”)

這種類型的代碼不起作用。 它說未知的選項“-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()

因為你正在進行全局導入(很少有好主意),並且因為你在tkinter之后導入了ttk。 兩個庫都定義了一個Button小部件,因此ttk Button會覆蓋tkinter Button ttk Button沒有foreground選項。

您應該停止使用全局導入來消除此問題:

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

所以看看這里你可以看到“前景”和“fg”選項是一樣的。 但是這只是python3的新版tkinter中的情況,如果你使用舊版本的python2.7,你必須使用“fg”選項。

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

如果您想在之后更改文本顏色,可以使用config函數實現此目的:

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

我希望這可以解決一些問題。 保持編碼; D.

我用fg

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

編輯:嗯,實際上, fgforeground都適合我。 如果你不打擾顏色,其他一切工作嗎? 可能是某些其他錯誤正在傳播下來。 以下是使用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()

在Python 2.7中試試這個:

將Tkinter導入為tk

所以Tkinter用大寫的T.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM