Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call a function and pass variable in tkinter scale widget?

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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())))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading