简体   繁体   English

无法在Tkinter中更改Frame小部件的背景颜色?

[英]Unable to change background color of Frame widget in Tkinter?

I'm using Python and Tkinter to create a game, and I'm attempting to have different "screens" (main menu and a level editor so far) which are each a tkinter.Frame object, and in the class definition for the main menu screen ( ScreenMainMenu ), in __init__ , on line 11, I'm attempting to use self.config() in order to change the width, height, and background color of the "main menu" tkinter.Frame . 我正在使用Python和Tkinter来创建游戏,并且试图拥有不同的“屏幕”(到目前为止,主菜单和关卡编辑器),每个tkinter.Frame都是tkinter.Frame对象,并且在主类的类定义中菜单屏幕( ScreenMainMenu ),在第11行的__init__ ,我尝试使用self.config()来更改“主菜单” tkinter.Frame的宽度,高度和背景颜色。 But when I run this code, the background is still grey. 但是当我运行这段代码时,背景仍然是灰色的。 I'm guessing I am missing something obvious (I am still fairly new to Tkinter and classes). 我猜想我遗漏了一些明显的东西(对于Tkinter和班级我还是很新的)。 Any help would be much appreciated - thank you. 任何帮助将不胜感激-谢谢。

import tkinter as tk

WIDTH = {"MainMenu": 600, "Editor": 450}
HEIGHT = {"MainMenu": 500, "Editor": 501}

class ScreenMainMenu(tk.Frame):
  def __init__(self, parent, *args, **kwargs):
    tk.Frame.__init__(self, parent, *args, **kwargs)
    parent.parent.geometry("{}x{}".format(WIDTH["MainMenu"], HEIGHT["MainMenu"]))
    parent.config(width=WIDTH["MainMenu"], height=HEIGHT["MainMenu"])
    self.config(width=WIDTH["MainMenu"], height=HEIGHT["MainMenu"], background="red")
    self.grid()
    self.create_widgets()

  def create_widgets(self):
  #  self.logo = tk.Label(self, image=r"res\logo.png")
    self.logo = tk.Label(self, text="Build The Galaxy")
    self.button_new_game = tk.Button(self, text="New Game")
    self.button_load_game = tk.Button(self, text="Load Game")
    self.button_editor = tk.Button(self, text="Editor")
    self.button_exit = tk.Button(self, text="Exit")

    self.logo.grid(sticky="EW")
    self.button_new_game.grid(row=1, sticky="EW")
    self.button_load_game.grid(row=2, sticky="EW")
    self.button_editor.grid(row=3, sticky="EW")
    self.button_exit.grid(row=4, sticky="EW")

class ScreenEditor(tk.Frame):
  def __init__(self, parent, *args, **kwargs):
    tk.Frame.__init__(self, parent, *args, **kwargs)
    self.grid()
    parent.parent.geometry("{}x{}".format(WIDTH["Editor"], HEIGHT["Editor"]))

class MainApplication(tk.Frame):
  def __init__(self, parent, *args, **kwargs):
    tk.Frame.__init__(self, parent, *args, **kwargs)
    self.parent = parent
    self.grid()
    self.screen_open_main_menu()

  def screen_open_main_menu(self):
    self.screen_mainmenu = ScreenMainMenu(self)

  def screen_open_editor(self):
    self.screen_editor = ScreenEditor(self)

if __name__ == "__main__":
  root = tk.Tk()
  root.resizable(False, False)
  root.title("Build The Galaxy")
  main_app = MainApplication(root)
  root.mainloop()

In Tkinter, normally all widgets adjust their size to fit the contents. 在Tkinter中,通常所有小部件都会调整其大小以适合内容。 That means, your frame is actually red (or whatever), it just fits its content. 这意味着您的框架实际上是红色(或其他颜色),恰好适合其内容。 (You can check it commenting out the self.create_widgets() method.) You can force the size with the .grid_propagate() method, passing a 0 as parameter: (您可以检查它是否对self.create_widgets()方法进行注释。)您可以使用.grid_propagate()方法强制大小,并传递0作为参数:

class ScreenMainMenu(tk.Frame):
  def __init__(self, parent, *args, **kwargs):
    tk.Frame.__init__(self, parent, *args, **kwargs)
    parent.parent.geometry("{}x{}".format(WIDTH["MainMenu"], HEIGHT["MainMenu"]))
    parent.config(width=WIDTH["MainMenu"], height=HEIGHT["MainMenu"])
    self.config(width=WIDTH["MainMenu"], height=HEIGHT["MainMenu"], background="red")
    self.grid()
    self.create_widgets()
    self.grid_propagate(0)  # force the widget to a certain size

BTW, you can use super() to initialize the parent: 顺便说一句,您可以使用super()初始化父级:

super().__init__(parent)  # yes, without self!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM