python - Tkinter - 在界面中添加背景但只在根目录中接受?
问题描述
我必须模块 Frames.py 和 view.py。
我需要background='#bfbfbf'
在项目中添加,但只在 root 中添加?
代码 view.py 是:
from tkinter import Toplevel, Label, Message, ttk
from content import frames
from tkinter.ttk import *
class MainPage():
"""Main interface"""
def __init__(self, master=None, **kw):
super().__init__(**kw)
self.root = master # Main window
self.root.resizable(False, False)
self.root.geometry('300x300')
self.root.config(background='#bfbfbf') # this add just outside project
self.style = ttk.Style()
self.style.theme_use('clam')
self.style.configure('TLabel', background='#bfbfbf')
self.style.configure('TButton', background='#bfbfbf')
代码 Frames.py 是:
from tkinter import Button, Label, Frame
from tkinter.ttk import *
class HomeFrame(Frame): # Inherit Frame class
"""Application main interface"""
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent)
super().__init__(**kw)
self.root = parent # Define internal variable root
self.home_page()
def init_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)
这是结果:
此代码的任何编辑?
解决方案
代码 view.py 是:
from tkinter import Label, Message, ttk
class MainPage():
"""Main interface"""
def __init__(self, master=None):
super().__init__()
self.root = master # Main window
#self.root.resizable(False, False)
#set_window_center(self.root, 660, 400)
self.root.geometry('300x300')
self.root.config(background='#bfbfbf')
self.style = ttk.Style()
print(self.style.theme_names())
print(self.style.theme_use())
self.style.theme_use('clam')
print(self.style.theme_use())
self.style.configure('TFrame', background=self.root.cget("bg"))
self.style.configure('TLabel', background='#bfbfbf')
self.style.configure('TButton', background='#bfbfbf')
代码 Frames.py 是:
from tkinter import Button, Label
from tkinter.ttk import * # If Remove this line , the background not working
class HomeFrame(Frame): # Inherit Frame class
"""Application main interface"""
def __init__(self, parent=None):
super().__init__(parent)
#self.root = parent # Define internal variable root
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)
from tkinter.ttk import *
从模块 Frames.py 中删除后
推荐阅读
- sphinx - Thinking Sphinx(v4.4) - 在索引定义中包含“where”条件后索引时出错
- bluetooth - 连接到网络蓝牙
- entity-framework - 如何消除由于 SQL Server 2017 中标识列的跳转而导致的空白?
- python - 在python中使用Azure语音服务读取音频文件并转换为文本,但只有第一句话被转换为语音
- java - 如何在 android studio 中的片段上使用 beginTransaction.hide().show() 而不是 beginTransaction().replace()?
- statsmodels - 训练 statsmodels GLM 模型时在权重中检测到 NaN、inf 或无效值检测错误
- php - 无法通过 PHP shell_exec 在 apache 下运行 go 二进制文件
- java - 如何将数据库(例如 MongoDb)中的外部数据插入 Cucumber Feature 文件的场景大纲?甚至可能吗?
- c# - 如何使用 Moq 和 xUnit 测试异步方法
- python - 替换布尔 Numpy 数组中 True 项的百分比