簡體   English   中英

Tkinter 在制作新框架時無法准確反映 kwargs。 如何更改繼承框架背景顏色?

[英]Tkinter won't accurately reflect kwargs when making a new frame. How do I change inheriting frame background color?

在我的代碼中,我創建了一個名為HistoryFrame的 object ,它應該繼承自tkinter.Frame 我想將我的HistoryFrame的背景顏色更改為在其創建時傳入的參數,並且當我打印 kwargs 時,它准確地反映了我在 {'bg':'blue', etc...} ,但它不會改變框架背景的實際顏色。

我該如何解決? 為什么會這樣?

以下代碼生成的應用程序圖片。 代碼如下。 V---V

import tkinter as tk

class HistoryFrame(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        print(kwargs)
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        button = tk.Button(self, text="hello", fg="blue")
        button.pack(side=tk.TOP, fill=tk.X)



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

        self.width, self.height = max(parent.winfo_screenwidth(), 200), max(
            parent.winfo_screenheight(), 200
        )

        self.history_frame = HistoryFrame.HistoryFrame(
            self, width=max(20, self.width // 4), height=self.height, bg="blue"
        )
        self.history_frame.pack(side=tk.LEFT, fill=tk.X)


if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("650x250")
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

你的顏色工作正常。 默認情況下,當您使用packgrid將一個或多個小部件添加到框架時,框架將縮小以適合其子級。 顏色在那里,只是框架被按鈕隱藏了。

您可以通過更改包裝框架的方式來觀察這一點:

self.history_frame.pack(side=tk.LEFT, fill="both", expand=True)

截屏

暫無
暫無

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

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