python-3.x - Tkinter Trace Method returns 3 variables when 0 are expected
问题描述
I am trying to make a dynamic gui that changes its labels based on what value is selected in a combo box. I am trying to employ the .trace method to accomplish this. I am receiving the following error with my current set up "area_labels takes 0 positional arguments but 3 were given)
I have attached the relevant parts of my code below.
from tkinter import *
import tkinter as tk
from tkinter import ttk
window = Tk()
window.geometry('1300x700')
window.title('Volumetric Calculator')
areastcboxv = StringVar()
areastcbox = ttk.Combobox(window, textvariable = areastcboxv, values = ('Discrete','Normal','Truncated Normal', 'Log Normal'))
areastcbox.grid(row = 1, column = 2)
areastcbox.set('Discrete')
areastcboxv.trace("w", area_labels)
def area_labels():
if areastcboxv.get() == "Discrete":
lvlabel = Label(text = 'Base Case Value', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 3)
bvlabel = Label(text = 'Standard Dev', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 4)
hvlabel = Label(text = 'High Value', fg = 'black', bg = 'white', width = 17).grid(row = 0, column = 5)
lplabel = Label(text = 'Low Probability', fg = 'black', bg = 'white', width = 17).grid(row = 0, column = 6)
bplabel = Label(text = 'Base Probability', fg = 'black', bg = 'white', width = 17).grid(row = 0, column = 7)
hplabel = Label(text = 'High Probability', fg = 'black', bg = 'white', width = 17, state = DISABLED ).grid(row = 0, column = 8)
elif areastcboxv.get() == "Normal":
lvlabel = Label(text = 'Base Case Value', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 3)
bvlabel = Label(text = 'Standard Dev', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 4)
hvlabel = Label(text = 'High Value', fg = 'black', bg = 'white', width = 17, state = DISABLED ).grid(row = 0, column = 5)
lplabel = Label(text = 'Low Probability', fg = 'black', bg = 'white', width = 17, state = DISABLED ).grid(row = 0, column = 6)
bplabel = Label(text = 'Base Probability', fg = 'black', bg = 'white', width = 17, state = DISABLED ).grid(row = 0, column = 7)
hplabel = Label(text = 'High Probability', fg = 'black', bg = 'white', width = 17, state = DISABLED ).grid(row = 0, column = 8)
elif areastcboxv.get() == "Truncated Normal":
pass
elif areastcboxv.get() == "Log Normal":
pass
window.mainloop()
解决方案
跟踪将提供三个参数。将您的功能更改为 area_labels(*event) 或 area_labels(a,b,c)
在这里寻找解释Tkinter 变量跟踪方法回调的参数是什么?
import tkinter as tk
from tkinter import ttk
def selection(*trace_event):
print('the trace arguments',trace_event)
chosen['text'] = options.get()
ROOT = tk.Tk()
val = tk.StringVar()
options = ttk.Combobox(textvariable=val,
values=('a','b','c'))
options.grid()
val.trace('w', selection)
chosen = tk.Label(text='No selection')
chosen.grid()
ROOT.mainloop()
推荐阅读
- python - 将模型从 Maya 导入 Unity 将所有内容放在位置 0
- java - 为什么我得到“找不到符号:方法 toUnmodifiableList()”运行时错误?
- java - 在服务器 open-api 上看不到 swagger-ui.html
- amazon-cloudformation - 使用 CloudFormation 在 Route53 上基于其他 HealthCheck 创建 HealthCheck
- python - 编译器在此 kivy python 代码中显示语法错误。但我没有找到任何
- sorting - 用芬威克树计算子数组和
- html - 如何使用 jQuery 添加多个元素?
- python - 如何根据第二个数据框在第一个数据框中创建新列?
- docker - 为开发和生产环境管理软件依赖项的最佳实践
- python - AttributeError:“StandardFrame”对象没有属性“tk”