如何从其他类配置tkinter标签和框架颜色?

时间:2017-01-12 23:33:50

标签: python class tkinter widget frames

首先,我刚刚开始编码,这是我的学校项目。我无法改变不同类别的帧和标签的颜色。这可能看起来像一个菜鸟问题,我知道我的编程非常糟糕,但任何帮助都会受到重视。

class Menubar(tk.Menu):         #Creates a class called Menubar
    def __init__(self, master):     #Initializes the class.

        menu = tk.Menu(root)        #Creates a Menu with variable menu inside root.
        root.config(menu=menu)      #Configures it to a menu.

        subMenu = tk.Menu(menu)     #Creates the sub menu inside menu.
        menu.add_cascade(label = "File", menu = subMenu)        #Creates a drop down called File, in the submenu.
        subMenu.add_command(label='New Day...', command = self.newday)  #Creates a labels with a function newday.
        subMenu.add_command(label='Quit', command = quit)       #Creates a labels with a function quit.

        subMenu2 = tk.Menu(menu)    #Creates a second submenu in the main menu.
        menu.add_cascade(label = 'Theme Options', menu = subMenu2)    #Creates a drop down in the second sub menu.
        subMenu2.add_command(label = 'White Theme', command = self.whitetheme) #Creates the label White Theme and when clicked it will a command.
        subMenu2.add_command(label = 'Dark Theme', command = self.darktheme) #Creates the label Dark Theme and when clicked it will a command.

    def newday(self):
        #Variable t stores the name that is given to the spread sheet file.
        t = str(tm.strftime("%d-%m-%Y")) + ' - ' + str(tm.strftime("%A")) + '.xlsx'
        wb = xl.Workbook()      #wb creates a new spread sheet file.
        ws = wb.active      #ws grabs the active worksheet in the spread sheet.

        labels = (              #This array stores the labels and formula that will be printed to the spread sheet.
            ['Bags', ''],
            ['Notes', ''],
            ['£2', ''],
            ['£1', ''],
            ['50p', ''],
            ['20p', ''],
            ['10p', ''],
            ['5p', ''],
            ['2p', ''],
            ['1p', ''],
            ['Total Till', '=SUM(B3:B12)'],
            ['Previous Till', ''],
            ['Till','=B13-B14'],
            ['Card', ''],
            ['Cash', ''],
            ['Day Total', '=SUM(B15:B17)'],
        )

        r = 3   #Variable r stores value 3
        c = 1   #Variable c stores value 1

        for text, formulae in labels:               #The for loop will be used to print the array data in to the spread sheet.
            ws.cell(row=r, column=c, value=text)    #This will print the labels in to the rows.
            ws.cell(row=r, column=c+1, value = formulae)    #This will print the formula in to the column.
            r += 1

        wb.save(t)  #Saves the workbook usung the name in variable t

        msgbox.showinfo('Daily Accounts', 'New Day created.') #Outputs a messagebox that tells the user a new day is created.

    def whitetheme(self):
        varframe = frame.mainlabels(self)
        self.lblBags.configure(bg = "black")

    def darktheme(self):
        self.frame.configure(bg = "black")

我希望能够点击暗和亮主题选项,它会改变下面课程中框架和标签的颜色。

 class frame(tk.Frame):  #Creates the class frame where the widgets will be held.
    def __init__(self, master):     #Initializes the class.
        self.frame = tk.Frame(master)        #A frame is created.
        self.frame.configure(bg = "white")   #Frame background colour changed to white.
        self.frame.pack(fill="both", expand = True)      #Places the frame and fills to the window size.

        self.toplabels() #Calls the function toplabels which houses the labels for the top of the program.
        self.mainlabels() #Calls the function mainlabels which has all the labels for the program.
        self.entryfields() #Calls the function entryfields which will create the entry fields for the user
                            #to enter the values.
        self.buttons() #Calls the function buttons which will add the buttons to the main frame.

    def toplabels(self):        #This function creates the top labels.
        self.lblShopName = tk.Label(self.frame, text = 'Bargain Food & Wine', background = 'white') #Creates a label in the frame with the store name.
        self.lblShopName.pack() #Positions the label in the centre.

        self.lblCurrentDay = tk.Label(self.frame, text = "Day: " + tm.strftime("%A"), background = 'white') #Creates a label in the frame with the current day.
        self.lblCurrentDay.place(x = 0 , y = 0) #Positions the label in the top left.

        self.lblTime = tk.Label(self.frame, text = "Date: " + tm.strftime("%d/%m/%Y"), background = 'white')    #Creates a label in the frame with the current date.
        self.lblTime.place(x = 380, y = 0)  #Positions the label in the top right.

    def mainlabels(self):   #Function creates the mainlabels for the programs.
        self.lblBags = tk.Label(self.frame, text = "Bags: ", bg = "white")                      #All the labels will be created in the frame and will have a white background.
        self.lblBags.place(x = 20, y = 40)                                                      #All the labels will placed in the frame using the place gemetry and co ordinates.
        self.lblNotes = tk.Label(self.frame, text = "Total Notes: ", bg = "white")
        self.lblNotes.place(x = 20, y = 60)
        self.lbl2pound = tk.Label(self.frame, text = "£2: ", bg = "white")
        self.lbl2pound.place(x = 20, y = 80)
        self.lbl1pound = tk.Label(self.frame, text = "£1: ", bg = "white")
        self.lbl1pound.place(x = 20, y = 100)
        self.lbl50p = tk.Label(self.frame, text = "50p: ", bg = "white")
        self.lbl50p.place(x = 20, y = 120)
        self.lbl20p = tk.Label(self.frame, text = "20p: ", bg = "white")
        self.lbl20p.place(x = 20, y = 140)
        self.lbl10p = tk.Label(self.frame, text = "10p: ", bg = "white")
        self.lbl10p.place(x = 20, y = 160)
        self.lbl5p = tk.Label(self.frame, text = "5p: ", bg = "white")
        self.lbl5p.place(x = 20, y = 180)
        self.lbl2p = tk.Label(self.frame, text = "2p: ", bg = "white")
        self.lbl2p.place(x = 20, y = 200)
        self.lbl1p = tk.Label(self.frame, text = "1p: ", bg = "white")
        self.lbl1p.place(x = 20, y = 220)
        self.lblTotalTill = tk.Label(self.frame, text = "Total Till: ", bg = "white")
        self.lblTotalTill.place(x = 20, y = 240)
        self.lblTotalTillDisplay = tk.Label(self.frame, text = '£000.00', bg = "white")
        self.lblTotalTillDisplay.place(x = 100, y = 240)
        self.lblPrevTill = tk.Label(self.frame, text = "Previous Till: ", bg = "white")
        self.lblPrevTill.place(x = 20, y = 260)
        self.lblLeftTill = tk.Label(self.frame, text = "Final Till: ", bg = "white")
        self.lblLeftTill.place(x = 20, y = 280)
        self.lblLeftTillDisplay = tk.Label(self.frame, text = '£00.00', bg = "white")
        self.lblLeftTillDisplay.place(x = 100, y = 280)
        self.lblCard = tk.Label(self.frame, text = "Card: ", bg = "white")
        self.lblCard.place(x = 20, y = 300)
        self.lblCash = tk.Label(self.frame, text = "Cash: ", bg = "white")
        self.lblCash.place(x = 20, y = 320)
        self.lbldaytotal = tk.Label(self.frame, text = 'Day Total: ', bg = 'white', font = 16)
        self.lbldaytotal.place(x = 300, y = 70)
        self.lbldaytotal2 = tk.Label(self.frame, text = '£000.00', bg = 'white', font = 16)
        self.lbldaytotal2.place(x = 380, y = 70)

对于长代码很抱歉,我很确定它可以做得更简单,但我刚开始。任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

我写了一个例子来向你展示如何使用ttk样式。你创建了自己的类,继承了tkinter小部件但是你没有在__init__方法中使用它,所以我的菜单栏类给你一个例子。

import tkinter as tk
import tkinter.ttk as ttk

class Menubar(tk.Menu):
    def __init__(self, master):
        # create the menu using tk.Menu init method
        tk.Menu.__init__(self, master)
        # display menu on the master window
        master.configure(menu=self)

        # create style used to change theme
        self.style = ttk.Style(master)
        self.whitetheme()

        subMenu = tk.Menu(self, tearoff=False)
        self.add_cascade(label='Theme Options', menu=subMenu) 
        subMenu.add_command(label ='White Theme', command = self.whitetheme) 
        subMenu.add_command(label ='Dark Theme', command = self.darktheme)

    def whitetheme(self):
        self.style.configure('TFrame', background='white')
        self.style.configure('TLabel', background='white', foreground="black")

    def darktheme(self):
        self.style.configure('TFrame', background='black')
        self.style.configure('TLabel', background='black', foreground="white")


if __name__ == "__main__":
    root = tk.Tk()

    Menubar(root)
    # use ttk frames and labels
    frame = ttk.Frame(root)
    frame.pack(fill='both', expand=True)

    ttk.Label(frame, text="Your text here").pack(padx=20, pady=20)

    root.mainloop()