当IntVar高于5时改变背景颜色(tkinter / python)

时间:2018-04-12 14:55:25

标签: python tkinter

我的问题是,当字符串变量高于5时,我正在尝试使背景颜色发生变化。 这是代码:

colour_p1="White"
kills_p1 = IntVar()
kills_p1.set(0)
def kill_count_p1():
    kills_p1.set(kills_p1.get()+1)
if kills_p1.get() >= 5:
    colour_p1 = "Yellow"
Label(master, textvariable=kills_p1, bg=colour_p1, font=("Courier", 20)).grid(column = 4, row=2)

我尝试了几种不同的方式,如" if",但它始终保持相同的颜色。当变量" kills_p1"时,我希望背景为黄色。高于5。 PS。这只是项目的一部分而不是整个

2 个答案:

答案 0 :(得分:1)

kill_count_p1中,您需要更新标签的颜色,否则不会更改:

def kill_count_p1():
    kills_p1.set(kills_p1.get()+1)
    if kills_p1.get() >= 5:
        colour_p1 = "Yellow"
        Label(master, textvariable=kills_p1, bg=colour_p1, font=("Courier", 20)).grid(column = 4, row=2)

然后,当您致电kill_count_p1时,如果colour_p1大于或等于5,则应更新标签颜色。

答案 1 :(得分:0)

为此,请创建一个按钮:

 Dim di as New DirectoryInfo("path here") ''Do not include additional filter parameters here

 For each file as FileInfo in di.EnumerateFiles
    file.Delete()  '''Deletes the files
 Next
 For each Folder as DirectoryInfo in di.EnumerateDirectories
    Folder.Delete(True)   '''Deletes the folders
 Next

创建条目:

button = tk.Button(root, text="click me", command=lambda:onclick1())
button.pack()

让以下是使用按钮调用的函数,该按钮在检查变量后更改背景:

x=IntVar()

e=Entry(root, textvariable=x)
e.pack()