【问题标题】:Python/Tkinter root window background configurationPython/Tkinter 根窗口后台配置
【发布时间】:2012-06-04 20:18:52
【问题描述】:

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

我有以下几点:

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') 在我的电脑上运行良好。

标签: python window tkinter root configure


【解决方案1】:

这可行(检查如何引用父根):

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

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()

【讨论】:

  • 您是否考虑了所有更改?检查图像。注意我使用了红色背景
  • 是的......代码确实有效,但背景仍然拒绝改变。也许我错过了包裹的一部分?编辑:刚刚更新了 tkinter 软件包,它仍然无法正常工作。
  • 你想改变什么背景?变成了红色!
【解决方案2】:

.configure 行中的“背景”不是“背景”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2018-05-29
    • 2020-01-29
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多