Search code examples
python-3.xtkintermacos-sierrattk

Ttk Frame, Background color


I'm using ttk, for my GUI. I know that it is also a very simple question ... I am trying to change the background color of the main window. I tried to change the theme, because I am working on a Mac, (and Python 3.5) to avoid the problem with the theme 'aqua', which is the default.I've been reading about several solutions like these questions which are about the same problem... These are the numbers of the questions:

54476511, 38712352, 47327266, 23750141.

But, I haven't Solve the problem, yet. Here it's my code.

#!/usr/bin/env python

# -*- coding: utf-8 -*-



from tkinter.scrolledtext import *

from tkinter import Tk, BOTH, W, N, E, S, messagebox, END

from tkinter.ttk import Button, Label, Style, Frame



class Example(Frame):


    def __init__(self,master):

        super().__init__()

        self.initUI()


    def initUI(self):

        self.master.title("Example")

        Style().theme_use("classic")

        self.pack(fill=BOTH, expand=1)


        self.columnconfigure(1, weight=1)

        self.columnconfigure(3, pad=7)

        self.rowconfigure(3, weight=1)

        self.rowconfigure(5, pad=7)


        self.txt_Pad = ScrolledText(self)

        self.txt_Pad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)

        self.txt_Pad.insert(END,'Type your info here')


        btn_save = Button(self, text="Save", command=self.save_command)

        btn_save.grid(row=1, column=3)


        btn_close = Button(self, text="Close", command=self.onClose)

        btn_close.grid(row=2, column=3, pady=4)


        btn_help = Button(self, text="Help", command=self.about_command)

        btn_help.grid(row=5, column=0, padx=5)        


    def onClose(self):

        self.master.destroy()


    def about_command(self):

        msb = messagebox.showinfo("About", "\"Insert a useful tip Here\"")


    def save_command(self):

        print('Your info it\'s save now')


    def open_command(self):

        print('Choose your File')



def main():

    root = Tk()

    root.geometry("350x300+300+300")

    root.configure(bg='#0059b3')

    app = Example(root)

    root.mainloop()




if __name__ == '__main__':

    main() 

Any Suggestions would be appreciated.


Solution

  • Create a style then apply it.

    from tkinter.scrolledtext import *
    
    from tkinter import Tk, BOTH, W, N, E, S, messagebox, END
    
    from tkinter.ttk import Button, Label, Style, Frame
    
    
    
    class Example(Frame):
    
    
        def __init__(self, master):
    
            super().__init__()
    
            self.initUI()
    
    
        def initUI(self):
    
            self.master.title("Example")
    
            # create a new style
            self.style = Style()
            # configure it to the background you want
            self.style.configure('My.TFrame', background='#0059b3')
            #Style().theme_use("classic")
            # apply it
            self.config(style='My.TFrame')
    
            self.pack(fill=BOTH, expand=1)
    
    
            self.columnconfigure(1, weight=1)
    
            self.columnconfigure(3, pad=7)
    
            self.rowconfigure(3, weight=1)
    
            self.rowconfigure(5, pad=7)
    
    
            self.txt_Pad = ScrolledText(self)
    
            self.txt_Pad.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
    
            self.txt_Pad.insert(END,'Type your info here')
    
    
            btn_save = Button(self, text="Save", command=self.save_command)
    
            btn_save.grid(row=1, column=3)
    
    
            btn_close = Button(self, text="Close", command=self.onClose)
    
            btn_close.grid(row=2, column=3, pady=4)
    
    
            btn_help = Button(self, text="Help", command=self.about_command)
    
            btn_help.grid(row=5, column=0, padx=5)        
    
    
        def onClose(self):
    
            self.master.destroy()
    
    
        def about_command(self):
    
            msb = messagebox.showinfo("About", "\"Insert a useful tip Here\"")
    
    
        def save_command(self):
    
            print('Your info it\'s save now')
    
    
        def open_command(self):
    
            print('Choose your File')
    
    
    
    def main():
    
        root = Tk()
    
        root.geometry("350x300+300+300")
    
        root.configure(background='#0059b3')
    
        app = Example(root)
    
        root.mainloop()
    
    
    
    
    if __name__ == '__main__':
    
        main() 
    
    

    I left comments at the parts I changed.