2016-05-25 13 views
0

Ich mache eine Multiwindow-GUI in Tkinter und ich möchte die Hintergrundfarbe der GUI ändern. Ich habe mehrere vorgeschlagene Lösungen ausprobiert und sie funktionieren nicht. Zum Beispiel root.configure (Hintergrund = 'rot'). Das funktioniert nicht, entweder: http://www.java2s.com/Code/Python/GUI-Tk/SetFramebackgroundcolor.htmPython: Ändern Farbe des Rahmens root.configure (Hintergrund = 'rot') funktioniert nicht

import tkinter as tk 
from tkinter import * 
from tkinter import ttk 

LARGE_FONT = ("Verdana", 12) 

def loadmsg(msg): 
    popupload.fileName = filedialog.askopenfilename(filetypes=(("textfiler", ".txt"), ("Alla filer", ".*"))) 


class Dod(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     tk.Tk.wm_title(self, "GUi") 
     container = tk.Frame(self) 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 
     for F in (StartPage, PageOne): 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame(StartPage) 

    def show_frame(self, cont): 
     frame = self.frames[cont] 
     frame.tkraise() 



class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 
     ttk.Frame.__init__(self, parent) 
     label = ttk.Label(self, text="GUI", font=LARGE_FONT) 
     label.pack(pady=10, padx=10) 
     button = ttk.Button(self, text="Visit Page One", 
          command=lambda: controller.show_frame(PageOne)) 


     button2 = ttk.Button(self, text="Ok", 
          command=lambda: controller.label2) 

     button.pack() 
     button2.pack() 


class PageOne(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = ttk.Label(self, text="Page One", font=LARGE_FONT) 
     label.pack(pady=10, padx=10) 
     button1 = ttk.Button(self, text="Back to Home", 
          command=lambda: controller.show_frame(StartPage)) 

     button1.pack() 

root = Dod() 
root.geometry("800x400") 
root.mainloop() 
+0

Der Code, den Sie keinen Code geschrieben hat, die Hintergrundfarben zu ändern versucht. Was ist das Problem? Zeig uns, was du probiert hast. Beide Lösungen, die Sie in Ihrer Frage erwähnen, funktionieren. –

+0

Das ist mein Problem. Ich weiß nicht, wie man den Code implementiert ... –

Antwort

1

Dies ist sehr einfach ...

Alles, was Sie tun müssen, ist ein container.config() Argument in der Dod init Funktion der Klasse hinzufügen (tk.Tk) nach dem Argument container.grid_columnconfigure (0, Gewicht = 1). Fügen Sie dann frame.config (bg = "black") nach self.frames [F] = frame innerhalb der Schleife für F in (start1, start, P1, P2) hinzu. [Dadurch erhalten alle Frames in der Schleife eine schwarze Farbe ] Der Code ist wie folgt:

import tkinter as tk 
from tkinter import * 
from tkinter import ttk 

LARGE_FONT = ("Verdana", 12) 

def loadmsg(msg): 
    popupload.fileName = filedialog.askopenfilename(filetypes=(("textfiler", ".txt"), ("Alla filer", ".*"))) 


class Dod(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     tk.Tk.wm_title(self, "GUi") 
     container = tk.Frame(self) 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 
     F1.config() 

     self.frames = {} 
     for F in (StartPage, PageOne): 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.config(bg="black") 
      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame(StartPage) 

    def show_frame(self, cont): 
     frame = self.frames[cont] 
     frame.tkraise() 



class StartPage(tk.Frame): 
    def __init__(self, parent, controller): 
     ttk.Frame.__init__(self, parent) 
     label = ttk.Label(self, text="GUI", font=LARGE_FONT) 
     label.pack(pady=10, padx=10) 
     button = ttk.Button(self, text="Visit Page One", 
         command=lambda: controller.show_frame(PageOne)) 


     button2 = ttk.Button(self, text="Ok", 
         command=lambda: controller.label2) 

     button.pack() 
     button2.pack() 


class PageOne(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = ttk.Label(self, text="Page One", font=LARGE_FONT) 
     label.pack(pady=10, padx=10) 
     button1 = ttk.Button(self, text="Back to Home", 
         command=lambda: controller.show_frame(StartPage)) 

     button1.pack() 

root = Dod() 
root.geometry("800x400") 
root.mainloop() 
+0

Innerhalb der start.age .__ init__ Funktion rufen Sie die Eltern-Init mit ttk .__ init__ auf, obwohl die Klasse ein Abkömmling von tk.Frame, nicht ttk.Frame ist. Ist das gültig? – RufusVS

+0

Ich benutze Python 2.7 und festgestellt, dass ttk.Frames nicht bg als eines ihrer Konfigurationselemente haben. Ich änderte diese in tk.Frames und die Dinge funktionierten, bis ich die F1.config() Zeile oben traf, und den Verweis auf controller.label2 in der button2 Definition, die nicht definiert ist. – RufusVS

Verwandte Themen