0

I am trying to use tkinter scale widget to increase or decrease a Label widget text size on the canvas. I am using the the scale widget variable value to do so. The text size changing when the button is clicked (my_btn) but I want it to change and I would like to increase or decrease the text size by dragging the scale widget left or right. When I am dragging the scale widget the program is prompting an error:

TypeError: <lambda>() takes 0 positional arguments but 1 was given.

The code is shown below. Please tell me how can I control the text size by dragging the scale widget directly.

    ##### Imports #####
from tkinter import *
from tkinter import ttk

###################################################################
                         # FUNCTIONS #
###################################################################
def slide(mainCanvas, textSlider):
    slide.textLayer.place_forget()
    slide.textLayer = Label(mainCanvas, text="TEST", font=('Arial', int(textSlider.get())), bg='white', fg='red')
    slide.textLayer.place(relx=0.5, rely=0.5, anchor='center')

###################################################################
                           # MAIN #
###################################################################
def main():
    root = Tk()
    rootWidth = 500
    rootHeight = 510
    root.minsize(rootWidth, rootHeight)

    mainFrameCanvas = Frame(root, bg='white', width=rootWidth, height=350)
    mainFrameCanvas.pack(anchor='n')
    mainCanvas = Canvas(mainFrameCanvas, width=rootWidth, height=350, bg='white', relief='solid', borderwidth=1)
    mainCanvas.pack(fill='x', expand=1)

    mainFrameSlider = Frame(root, bg='white', width=rootWidth, height=150, relief='solid', borderwidth=1)
    mainFrameSlider.pack(anchor='s')


    slide.textLayer = Label(mainCanvas, text="TEST", bg='white', fg='red')
    slide.textLayer.place(relx=0.5, rely=0.5, anchor='center')


    var = DoubleVar()
    textSlider = Scale(mainFrameSlider, from_=10, to=30, variable=var, orient=HORIZONTAL, length=rootWidth*0.9, troughcolor='white', showvalue=1, bg='white', highlightbackground='white')
    textSlider.bind("<Button-1>", lambda: slide(mainCanvas, textSlider))
    textSlider.place(x=20, y=40)


    my_btn = Button(root, text="Click Me!", command=lambda: slide(mainCanvas, textSlider)).pack()


    root.mainloop()

###################################################################
                           # RUN #
###################################################################

def run():
    print('\nStart script')
    main()
    print('Finished script')

run()

1 Answer 1

1

Callback for an event binding expects an argument, the Event object. So

textSlider.bind("<Button-1>", lambda: slide(mainCanvas, textSlider))

should be changed to:

textSlider.bind("<Button-1>", lambda e: slide(mainCanvas, textSlider))

Note that you don't need to remove and recreate the label inside slide(), just update the font for the label:

def slide(mainCanvas, textSlider):
    slide.textLayer.config(font=('Arial', int(textSlider.get())))
Sign up to request clarification or add additional context in comments.

Comments

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.