Python/Tkinter根窗口后台配置

标签 python window tkinter root configure

我正在尝试创建一个黑色背景的根窗口以与我的按钮背景融合。

我有以下内容:

class Application(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()
...

    def initUI(self):
        self.outputBox = Text(bg='black', fg='green', relief=SUNKEN, yscrollcommand='TRUE')
        self.outputBox.pack(fill='both', expand=True)
        self.button1 = Button(self, text='button1', width=40, bg ='black', fg='green', activebackground='black', activeforeground='green')
        self.button1.pack(side=RIGHT, padx=5, pady=5)
        self.button2 = Button(self, text='button2', width=20, bg='black', fg='green', activebackground='black', activeforeground='green')
        self.button2.pack(side=LEFT, padx=5, pady=5)
...

def main():
    root = Tk()   
    root.geometry('1100x350+500+300')
    root.configure(background = 'black')
    app = Application(root)
    root.mainloop()  

但是 root.configure(background = 'black') 没有改变根窗口的背景颜色...有什么建议吗?

最佳答案

这有效(检查如何引用父根):

编辑:我编辑了代码和图形以明确设置颜色的位置:

from Tkinter import *

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.parent = master
        self.initUI()

    def initUI(self):
        self.outputBox = Text(self.parent, bg='yellow', height= 10, fg='green', relief=SUNKEN, yscrollcommand='TRUE')
        self.outputBox.pack(fill='both', expand=True)
        self.button1 = Button(self.parent, text='button1', width=20, bg ='blue', fg='green', activebackground='black', activeforeground='green')
        self.button1.pack(side=RIGHT, padx=5, pady=5)
        self.button2 = Button(self.parent, text='button2', width=25, bg='white', fg='green', activebackground='black', activeforeground='green')
        self.button2.pack(side=LEFT, padx=5, pady=5)

def main():
    root = Tk()
    app = Application(root)
    app.parent.geometry('300x200+100+100')
    app.parent.configure(background = 'red')
    app.mainloop()

main()

enter image description here

关于Python/Tkinter根窗口后台配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10887762/

相关文章:

JavaScript:2 个 window.opener.location.href 语句与 alert() 之间无法正常工作

delphi - 使桌面窗口在窗口周围绘制框架无效

Python:tkinter askyesno 方法打开一个空窗口

python - 如何将 PIL 与 Tkinter 一起使用?

python - 在 Python 中将对象放置在图层中 - Maya

python - 在 NumPy 数组上计算 "moving sum of counts"

python - 从列表变量到 Pandas 中的列

python - 我怎样才能让 'autodoc' 工作?

javascript - 使用 JavaScript onClick 关闭窗口

python - 为什么通过从另一个模块调用函数来显示来自不同模块中不同 GUI 的图像时会出现问题?