Tkinter在接口中添加背景,但只在根中接受?

2024-10-02 14:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须模块Frames.py和view.py。

我需要在项目中添加background='#bfbfbf',但只在根目录中添加

代码视图.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')

code 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

对这个代码有任何修改吗


Tags: namefrompyimportselfinitstyletkinter
1条回答
网友
1楼 · 发布于 2024-10-02 14:27:07

code 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')

code 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

从module Frames.py中删除from tkinter.ttk import *之后

enter image description here

相关问题 更多 >