Python/Tkinter根窗口背景配置

4

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

我有以下代码:

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')并没有改变根窗口的背景颜色... 有什么建议吗?

嗯,你确定那是问题吗?在我的电脑上,root.configure(background='black') 运行得很好。 - Joel Cornett
2个回答

8

这段代码是可行的(请查看父级根目录的引用):

编辑:我已经编辑了代码和图像,以明确颜色设置的位置:

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


你有没有考虑到所有的更改?请查看图片。注意我使用了红色背景。 - joaquin
是的...代码确实可以工作,但背景仍然无法改变。也许我缺少某个包的一部分?编辑:刚刚更新了tkinter包,但仍然无法工作。 - user1435947
你想要改变什么背景?它已经被改成红色了! - joaquin

0

在 .configure 行中应该使用 'bg' 而不是 'background'。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接