首页 > 解决方案 > 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()

标签: python-3.xtkinter

解决方案


跟踪将提供三个参数。将您的功能更改为 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()

推荐阅读