如何在ttk python中更改背景颜色

时间:2016-08-02 05:28:20

标签: python-3.x tkinter ttk

我已经制作了一个简单的gui age转换器应用程序,但我想将其bg颜色更改为黑色。 问题出在ttk frame.i不知道如何配置它的bg颜色。 我尝试了不同的方法但是没有用。 如果你们能提供帮助,我将不胜感激。 这是代码

from tkinter import *
from tkinter import ttk
from PIL import Image




def calculate(*args):
        try:
            age_sec = int(age.get())
            age_sec = age_sec * 12 * 365 * 24 * 60 * 60
            age_seconds.set(age_sec)
        except:
            age_seconds.set('Either the field is empty or \n value is not numeric.')

root = Tk()
root.title("Converter")
root.configure(background="black")
mainframe = ttk.Frame(root, padding = "6 6 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)

#mainframe['borderwidth'] = 2
#mainframe['relief'] = 'groove'

age = StringVar()
age_seconds  = StringVar()

ttk.Label(mainframe,  foreground = "#4D4E4F", text = "Enter your Age: ").grid(column = 1, row = 1, sticky = E)
age_entry = ttk.Entry(mainframe, width = 30, textvariable = age)
age_entry.grid(column = 2, row = 1, sticky = (W, E))

ttk.Label(mainframe, foreground = "#4D4E4F", text = "your age in seconds is ").grid(column = 1, row  = 2, sticky = (E))

ttk.Label(mainframe, textvariable = age_seconds, background = "lightyellow", foreground = "#727475", width = 30).grid(column = 2, row  = 2, sticky = W)


#Mouse Events...\\

butt_image = PhotoImage(file = 'images.gif')
ttk.Button(mainframe, compound = TOP, text = "Hit Now", image =butt_image,  cursor = "hand2", width= 30, command = calculate).grid(column = 2, row  = 4,  sticky = W)
l2 = ttk.Label(mainframe,foreground = "#4D4E4F", text = "Mouse Events: ").grid(column = 1, row = 3, sticky = E)
l = ttk.Label(mainframe,background = "lightyellow", foreground = "#727475", text = 'Measurement is starting...', width = 30)
l.grid(column = 2, row = 3, sticky = W)
l.bind('<Enter>', lambda e: l.configure(text = 'Moved Mouse Inside'))
l.bind('<Leave>', lambda e: l.configure(text = 'Mouse Moved Out'))
l.bind('<1>', lambda e: l.configure(text = 'left Mouse clicked'))
l.bind('<Double-1>', lambda e: l.configure(text = 'Double clicked'))
l.bind('<B1-Motion>', lambda e: l.configure(text = 'Left button drag to %d, %d' %(e.x, e.y)))
image = PhotoImage(file = 'waves.gif')
ttk.Label(mainframe, compound = CENTER, text = "Image text", font = ('roman', 9, 'normal'), foreground ='green', image = image).grid(column = 3, row = 1, sticky = (N, E))
#if '__name__' == '__main__':
for child in mainframe.winfo_children(): child.grid_configure(padx = 15, pady =  15)
age_entry.focus()
root.bind('<Return>', calculate)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

ttk.Frame没有background / bg选项。要更改它,您需要使用style

请参阅ttk.Frame Info

如果您真的不需要使用ttk.Frame,则可以切换到tkinter.Frame