问题描述
所以我只想更改框架的背景,以便可以正确地布局它们,而且似乎无法更改框架的样式。
style.configure('TFrame',background='red')
style.configure('Blue.TFrame',background='blue')
main_window = MainWindow(root,style='Blue.TFrame')
以上代码在红色背景中重新显示,而我需要将其更改为蓝色,并且如果不更改TFrame背景,则根本没有背景色。 我的MainWindow类确实是从ttk.Frame继承的,所以我不知道那是什么原因造成的...
最小可复制示例:
import tkinter as tk
from tkinter import ttk
class MainWindow(ttk.Frame):
def __init__(self,parent,*args,**kwargs):
super().__init__(parent)
self.parent = parent
# Search_company shortcut
self.search_comp = ttk.Entry(self)
self.search_comp.grid(row=0,column=0)
def main():
root = tk.Tk()
root.state('zoomed')
# Configuring styles
style = ttk.Style()
style.configure('TFrame',background='red')
style.configure('Blue.TFrame',background='blue')
main_window = MainWindow(root,style='Blue.TFrame')
main_window.grid(row=0,column=1,sticky='nsew')
root.mainloop()
if __name__ == "__main__":
main()
解决方法
您没有将style
选项传递给超类。必须是这样的:
super().__init__(parent,*args,**kwargs)