【问题标题】:Tkinter - Problem after add background in my projectTkinter - 在我的项目中添加背景后出现问题
【发布时间】:2020-05-04 13:36:29
【问题描述】:

大家好,我在项目中添加背景后遇到问题,我没有看到任何东西,但在调整大小后我看到背景:我不想在我的对象中使用 ttk。

这是我的代码:

from tkinter import (Button, Label, Frame)


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

  def __init__(self, parent=None):
    Frame.__init__(self, parent)
    self.root = parent  # Define internal variable root
    self.root.configure(background='green')
    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)

这是结果:

Interface without resize

调整大小后:

Interface After Resize

【问题讨论】:

  • 你可能想要self.configure(...
  • Tkinter 小部件始终是完全不透明的,并且不会从包含的小部件或窗口继承任何东西(例如背景颜色)。因此,您看到的是所有单个小部件的默认背景:没有任何窗口背景可见,因为它的自然大小刚好足以容纳所有小部件。
  • @jasonharper 谢谢它的工作如何在这个项目中使用样式主题而不使用 ttk,例如:ttk.Label 或 ttk.Button

标签: python python-3.x tkinter tkinter-canvas tkinter-entry


【解决方案1】:
from tkinter import (Button, Label, Frame)


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

   def __init__(self, parent=None):
       Frame.__init__(self, parent)
       self.root = parent  # Define internal variable root
       self.root.configure(background='green')
       self.config(background='green')
       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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    相关资源
    最近更新 更多