Tkinter - добавить фон в интерфейс, но просто принять его в root? - PullRequest
0 голосов
/ 05 мая 2020

Мне нужно добавить модули Frames.py и view.py.

Мне нужно добавить background='#bfbfbf' в проект, но добавить только root?

код view.py:

from tkinter import Toplevel, Label, Message, ttk
from content import frames
from tkinter.ttk import *

class MainPage():
"""Main interface"""

  def __init__(self, master=None, **kw):
      super().__init__(**kw)
      self.root = master # Main window
      self.root.resizable(False, False)
      self.root.geometry('300x300')
      self.root.config(background='#bfbfbf') # this add just outside project
      self.style = ttk.Style()
      self.style.theme_use('clam')
      self.style.configure('TLabel', background='#bfbfbf')
      self.style.configure('TButton', background='#bfbfbf')

код Frames.py:

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

class HomeFrame(Frame):  # Inherit Frame class
"""Application main interface"""

   def __init__(self, parent=None, **kw):
      Frame.__init__(self, parent)
      super().__init__(**kw)
      self.root = parent  # Define internal variable root
      self.home_page()

  def init_page(self):
    """Load control"""

      Label(self, text="First name").grid(row=0, column=0, padx=20, pady=20)
      Full_name = Button(self, text="Get Full name")
      Full_name.grid(row=0, column=1, columnspan=2, padx=20, pady=20)
      Label(self, text="Last name").grid(row=1, column=0, padx=20, pady=20)
      hello = Button(self, text="hello")
      hello.grid(row=3, column=2, columnspan=2, padx=20, pady=20)

Результат:

enter image description here

Любые изменения для этого кода?

1 Ответ

0 голосов
/ 06 мая 2020

код view.py:

from tkinter import Label, Message, ttk

class MainPage():
"""Main interface"""

def __init__(self, master=None):
    super().__init__()
    self.root = master # Main window
    #self.root.resizable(False, False)
    #set_window_center(self.root, 660, 400)
    self.root.geometry('300x300')
    self.root.config(background='#bfbfbf')
    self.style = ttk.Style()
    print(self.style.theme_names())
    print(self.style.theme_use())
    self.style.theme_use('clam')
    print(self.style.theme_use())
    self.style.configure('TFrame', background=self.root.cget("bg"))
    self.style.configure('TLabel', background='#bfbfbf')
    self.style.configure('TButton', background='#bfbfbf')

код Frames.py:

from tkinter import Button, Label
from tkinter.ttk import * # If Remove this line , the background not working

class HomeFrame(Frame):  # Inherit Frame class
"""Application main interface"""

def __init__(self, parent=None):
    super().__init__(parent)
    #self.root = parent  # Define internal variable root
    self.home_page()

def home_page(self):
    """Load control"""

    Label(self, text="First name").grid(row=0, column=0, padx=20, pady=20)
    Full_name = Button(self, text="Get Full name")
    Full_name.grid(row=0, column=1, columnspan=2, padx=20, pady=20)
    Label(self, text="Last name").grid(row=1, column=0, padx=20, pady=20)
    hello = Button(self, text="hello")
    hello.grid(row=3, column=2, columnspan=2, padx=20, pady=20)

enter image description here

После удаления from tkinter.ttk import * из модуля Frames.py

enter image description here

...