簡體   English   中英

無法在Tkinter中更改Frame小部件的背景顏色?

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

我正在使用Python和Tkinter來創建游戲,並且試圖擁有不同的“屏幕”(到目前為止,主菜單和關卡編輯器),每個tkinter.Frame都是tkinter.Frame對象,並且在主類的類定義中菜單屏幕( ScreenMainMenu ),在第11行的__init__ ,我嘗試使用self.config()來更改“主菜單” tkinter.Frame的寬度,高度和背景顏色。 但是當我運行這段代碼時,背景仍然是灰色的。 我猜想我遺漏了一些明顯的東西(對於Tkinter和班級我還是很新的)。 任何幫助將不勝感激-謝謝。

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()

在Tkinter中,通常所有小部件都會調整其大小以適合內容。 這意味着您的框架實際上是紅色(或其他顏色),恰好適合其內容。 (您可以檢查它是否對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

順便說一句,您可以使用super()初始化父級:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM