【问题标题】:Python: Tkinter, areas around labels and button will not change colourPython:Tkinter,标签和按钮周围的区域不会改变颜色
【发布时间】:2017-11-09 22:30:20
【问题描述】:
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 中创建了一个小型调查,我想知道为什么标签、输入框和我的按钮周围的某些背景区域没有改变颜色,不胜感激!

(获取数据目前并不意味着有背景颜色)

如果有人能告诉我如何在第一个框的顶部添加一个图像,我将不胜感激,谢谢!

edit:现在我添加了更改颜色的相同方法在我的顶框中完美运行。主机仍然坏了。

【问题讨论】:

  • 你期望的颜色是什么?

标签: python tkinter colors


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多