【问题标题】:How to Use a Setter Function to Check Whether a Function Parameter has Changed - Python如何使用 Setter 函数检查函数参数是否已更改 - Python
【发布时间】:2018-08-19 06:44:16
【问题描述】:

这是我的代码:

def DisplayFiles(which):
    contentLabel = tk.Label(window, text = '\n'.join(files[indexlist[which]][1:]))
    contentLabel.place(x = 150, y = 80)

我正在使用 Tkinter 并尝试在按下按钮时显示具有上述功能的文件。变量“which”是按钮的字符串名称。 “indexlist”是一个保存按钮名称索引的字典(我动态创建了它们)。我的问题是尝试显示两个不同按钮的文件。当我单击一个按钮时,上面的功能会显示文件。但是当我单击另一个按钮时,标签会显示在前一个按钮上。我正在研究 destroy() 方法,但我需要知道如何检查参数“which”何时更改。帮助将不胜感激!

另外,indexlist 和 files 的值也不是问题。我只是想找到一种方法来检查函数参数何时更改。谢谢!

【问题讨论】:

标签: python python-3.x python-2.7 tkinter parameter-passing


【解决方案1】:

不要每次都制作新标签,更新旧标签即可。

# make an empty Label
contentLabel = tk.Label(window)
contentLabel.place(x = 150, y = 80)

def DisplayFiles(which):
    # update the Label contents
    contentLabel.config(text = '\n'.join(files[indexlist[which]][1:]))

【讨论】:

  • 我没想到。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
相关资源
最近更新 更多