Ttk Frame, Цвет фона - PullRequest
       71

Ttk Frame, Цвет фона

0 голосов
/ 29 января 2020

Я использую ttk, для моего GUI. Я знаю, что это тоже очень простой вопрос ... Я пытаюсь изменить цвет фона главного окна. Я попытался изменить тему, потому что я работаю над Ma c, (и Python 3.5), чтобы избежать проблемы с темой 'aqua', которая используется по умолчанию. Я читал о нескольких решениях, таких как эти вопросы, относящиеся к той же проблеме ... Вот номера вопросов:

54476511, 38712352, 47327266, 23750141.

Но я пока не решил проблему , Вот мой код.

#!/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() 

Любые предложения будут оценены.

1 Ответ

2 голосов
/ 29 января 2020

Создайте стиль и примените его.

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

Я оставил комментарии к измененным частям.

...