【问题标题】:How to change background color in ttk python如何在ttk python中更改背景颜色
【发布时间】:2016-08-02 05:28:20
【问题描述】:

我制作了一个简单的 gui 年龄转换器应用程序,但我想将其背景颜色更改为黑色。 问题出在 ttk 框架中。我不知道如何配置它的 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()

【问题讨论】:

    标签: python-3.x tkinter ttk


    【解决方案1】:

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

    见ttk.Frame Info

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

    【讨论】:

    • 为什么不给一个可运行的例子如何使用样式?给定的链接已失效:(
    • 固定链接。对于样式用法,您可以查看offical docs
    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 2023-02-23
    • 2013-07-12
    • 1970-01-01
    • 2016-09-10
    • 2011-11-11
    • 1970-01-01
    • 2017-10-16
    相关资源
    最近更新 更多