0

I am working on creating a LabelFrame class using Tkinter, which creates a wrapper around a group of items I wish to include one by one, by pressing an add button, that calls a function to create more of that item.

I have the code running, where I can see the LabelFrame and addbuttun. But once I press the button the function being called I get an error:

addmeter() takes exactly 1 argument (0 given)

I need to this function to add a class inside the LabelFrame, and this is where I am stuck.

I have listed my code below.

from Tkinter import *

    root = Tk()
    root.title("LabelFrame with embedded add voltmeters")
    root.geometry("600x200+400+400")


    def addmeter(self):
            #Create frame for the voltmeter
        voltsmet1 = LabelFrame(self.master, text = "Volts")
            #add Text box for the serial output. 
        voltinfo = Text(voltsmet1, bg="BLACK",  height=10, width =20 )
            #add in reg command to find our data from queue and display it



            #packs the widgets on the grid for display
        voltsmet1.pack(side=LEFT, expand=True)
        voltinfo.pack(side=LEFT,  expand=True)      

    class wrapper(LabelFrame):
        def __init__(self,master):
            self.master = master
            self.create_wrapper()

        def create_wrapper(self):
            wrapper = LabelFrame(self.master, text = "Volt Meters")
            add_button = Button(wrapper, text="add", command=addmeter)
            wrapper.pack()
            add_button.pack()

    new= wrapper(root)
    root.mainloop()

1 Answer 1

2

Use lambda function:

add_button = Button(wrapper, text="add", command=lambda:addmeter(self))

EDIT:

Do you mean this ?

enter image description here

I use wrapper in lambda function

add_button = Button(wrapper, text="add", command=lambda:addmeter(wrapper))

and I remove .master in addmeter

Full code:

from Tkinter import *

root = Tk()
root.title("LabelFrame with embedded add voltmeters")
root.geometry("600x200+400+400")


def addmeter(parent):
        #Create frame for the voltmeter
    voltsmet1 = LabelFrame(parent, text = "Volts")
        #add Text box for the serial output. 
    voltinfo = Text(voltsmet1, bg="BLACK",  height=10, width =20 )
        #add in reg command to find our data from queue and display it



        #packs the widgets on the grid for display
    voltsmet1.pack(side=LEFT, expand=True)
    voltinfo.pack(side=LEFT,  expand=True)      

class wrapper(LabelFrame):
    def __init__(self,master):
        self.master = master
        self.create_wrapper()

    def create_wrapper(self):
        wrapper = LabelFrame(self.master, text = "Volt Meters")
        add_button = Button(wrapper, text="add", command=lambda:addmeter(wrapper))
        wrapper.pack()
        add_button.pack()

new= wrapper(root)
root.mainloop()

btw: I change name self to parent in addmeter() to make names more logical.

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

2 Comments

that worked on get the special widget created. Thank you. However, it places its outside the LabelFrame and below the add button. Anyway to get it inside the LabelFrame that says VOLTS? That is my goal. I have three more frames like this to create and fill with different widgets.
I dont mean in the Volts LabelFrame. I mean in the Volt Meter Frame. I need to fill that LabelFrame with the voltmeter widgets.

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.