Python:Tkinter,标签和按钮周围的区域不会改变颜色

时间:2017-11-09 22:30:20

标签: python tkinter colors

from tkinter import *



class MyWindow(Frame):
  def __init__(self,parent):
    Frame.__init__(self,parent)

    root.configure(bg = "cornsilk")



    def Exit():


        print("your name is", Name.get())
        print (f"You are {Age.get()} years old")
        print("Your favourite pastime is", Pastime.get())

        root.destroy()


    def GetData():


        def Restart():
            top.destroy()

        top = Toplevel()
        top.title("correct?")
        top.geometry("200x200")
        lbl = Label(top, text = "Is this all correct?", fg = "red")
        lbl.place(x=30, y = 0)

        Correct_Name = Label(top, text = f"your name is {Name.get()}.", fg = "blue" )
        Correct_Name.place(x=30, y=33)

        Correct_Age = Label(top, text = f"You are {Age.get()} years old.", fg ="blue")
        Correct_Age.place(x=30, y=66)

        Correct_Pastime = Label(top, text = f"You like {Pastime.get()}.", fg = "blue" )
        Correct_Pastime.place(x=30, y=99)

        Submit2 = Button(top, text = "yes", fg ="blue", command = Exit)
        Submit2.place(x = 30, y = 132)
        Submit3 = Button(top, text = "no", fg ="blue", command = Restart)
        Submit3.place(x = 60, y = 132)


    label1 = Label(self, text = "What is your full name?", fg="red")
    label1.grid()
    Name = Entry(self, fg = "blue")
    Name.grid()


    label2 = Label(self, text = "how old are you?", fg ="red")
    label2.grid()
    Age = Entry(self, fg = "blue")
    Age.grid()

    label3 = Label(self, text = "What is your favourite pastime?",fg = "red")
    label3.grid()
    Pastime = Entry(self, fg = "blue")
    Pastime.grid()



    Submit_All = Button(self, text = "submit", command = GetData)
    Submit_All.grid()

    myWidgets = [label1, label2,label3, Submit_All] # List of widgets to change colour
    for wid in myWidgets:
        wid.configure(bg = "cornsilk")


root = Tk()

MyWindow(root).grid()

root.title("Survey")
root.mainloop()

此代码在tkinter gui中创建一个迷你调查,我想知道为什么标签,输入框和我的按钮周围的某些背景区域没有改变颜色,非常感谢帮助!

(获取数据目前不具有背景颜色)

我也很感激,如果有人能告诉我如何在第一个盒子的顶部添加一个imae,谢谢!

编辑:在我添加它的同时,改变颜色的相同方法在我的顶部框中完美运行。主框架仍然坏了。

1 个答案:

答案 0 :(得分:0)

尽我所知,您需要将self添加到myWidgets,或在__init__中添加单独的语句。如果这不能回答你的问题,请澄清一下。