1

This is my first time creating a GUI using Tkinter to simulate a DSP setup. I'm stuck.

enter image description here

This GUI requires multiple scales. Therefore I wrote a generic scale function.

# Overlap percent scale  
def overlap_percent():
    print("Testing")
    # op=var.get()
    # olive_features['overlap_perc']=op

    # generic scale function
def create_scale(self,parent,xval,yval,start,end,resolution,orient,default,lng,clr,cmdtrig):
    OHA_scale = tk.Scale(parent,resolution=resolution, from_=start, to=end, orient=orient,length=lng,fg=clr,command=cmdtrig)
    OHA_scale.grid(row=xval, column=yval, sticky=tk.E + tk.W + tk.N + tk.S, padx=20, pady=4)
    OHA_scale.set(default)


    # Overlap %
    yval=yval+2
    xval=1
    self.create_label(Oliveframe,xval,yval,"Overlap %")
    xval=3
    self.create_scale(Oliveframe,xval,yval,25,75,25,"vertical",75,90,'Black',"overlap_percent")
    xval=1
    yval=yval+1
    self.create_arrow(Oliveframe,xval,yval)

Based on the code above, i figured it would print 'Testing' on the cmd line, when i adjust the Overlap scale. Nothing happened. To test the code, i replaced the command in generic scale to

def create_scale(self,parent,xval,yval,start,end,resolution,orient,default,lng,clr,cmdtrig):
    OHA_scale = tk.Scale(parent,resolution=resolution, from_=start, to=end, orient=orient,length=lng,fg=clr,command=overlap_percent)

This resulted in an error.

OHA_scale = tk.Scale(parent,resolution=resolution, from_=start, to=end, orient=orient,length=lng,fg=clr,command=overlap_percent) NameError: name 'overlap_percent' is not defined

I can't quite figure out what I'm doing wrong. How exactly can i use the generic scale function to control multiple scales?

10
  • where you modified the command function generic scale exactly Commented Sep 15, 2020 at 7:24
  • i modified my OP to highlight how i modified the command function. Commented Sep 15, 2020 at 7:44
  • 2
    If overlap_percent() is within the class, then self.overlap_percent should be used instead. Commented Sep 15, 2020 at 7:52
  • 1
    So you define overlap_percent() wrong, should be def overlap_percent(self, value) instead. Commented Sep 15, 2020 at 7:59
  • 1
    You can pass an extra argument, for example name, to create_scale(...): def create_scale(..., cmdtrig, name): and pass the name to the generic function: tk.Scale(..., command=lambda v: cmdtrig(name, v). Then define the generic function like def some_function(self, scale_name, value):. You can then create scales like create_scale(..., self.some_function, 'scale1'), create_scale(..., self.some_function, 'scale2') and etc. Commented Sep 15, 2020 at 8:22

1 Answer 1

2

on this line

OHA_scale = tk.Scale(parent,resolution=resolution, from_=start, to=end, 
orient=orient,length=lng,fg=clr,command=cmdtrig)

command take function parameters but you send a string value

self.create_scale(Oliveframe,xval,yval,25,75,25,"vertical",75,90,'Black',overlap_percent)

If you do it will be alright

Sign up to request clarification or add additional context in comments.

2 Comments

I did try that before I posted my question. It ends up with an error NameError: name 'overlap_percent' is not defined similar to what I showed in the OP when i modified the scale function. I thought it might be due to the sequential order . so i played around with the order, still no luck :(
yes. i tried self.overlap_percent . That resulted in " TypeError: overlap_percent() takes 0 positional arguments but 2 were given " . i didn't quite understand how 2 arguments were given.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.