2017-01-12 5 views
0

우선 나는 코딩을 시작했는데 이는 내 학교 프로젝트를위한 것입니다. 다른 클래스의 프레임과 레이블의 색상을 변경할 수없는 문제가 있습니다. 그것은 멍청한 질문처럼 보일지도 모르겠다. 내 프로그래밍이 상당히 나쁘다는 것을 알고 있지만 도움이 필요하다.다른 클래스의 tkinter 레이블 및 프레임 색상을 구성하는 방법은 무엇입니까?

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) 

죄송합니다. 긴 코드를 사용하면 매우 간단하지만 쉽게 시작할 수 있습니다. 어떤 도움이라도 대단히 감사하겠습니다. 감사합니다

+0

가 [테마 위젯] 참조 (https://docs.python.org/2/library/ttk.html#ttk-widgets) 및 스타일 – furas

답변

0

나는 ttk 스타일을 사용하는 방법을 보여주기위한 예제를 작성했습니다. 클래스를 만들고 tkinter 위젯을 상속 받지만 __init__ 메서드에서는 사용하지 않으므로 Menubar 클래스에서 예제를 제공합니다.

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