Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8127721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:37:47+00:00 2026-06-06T07:37:47+00:00

I’m having problems with the following code (I am a beginner in most things

  • 0

I’m having problems with the following code (I am a beginner in most things Python related) and am not sure how to use ‘self’ in this sense. I simply would like to retrieve any values given in the Entry boxes I create (bearing in mind there is a different amount of boxes depending on the if statement) when the start button is clicked.

I am however getting errors with ‘self’ not defined and such like. Is there any way of keeping the loops I’ve made and getting all the values back or is there a simpler way of writing this?

def ChBox(self, ch):

    column = 0
    if ch == "List":
        i = [0, 1, 2, 3]

        for i in i:
            ch_title = Label(ch_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0)
            ch_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1)

            self.box = Entry(ch_frame, bg='white', borderwidth=0, width=0)
            self.box.grid(row=1, column=column+i, sticky="ew", padx=1, pady=1)

    elif ch == "Inter":
        i = [0, 1]
        ch_title1 = Label(ch_frame, text='Min Value', bg='white', borderwidth=0, width=0)
        ch_title1.grid(row=0, column=column, sticky="nsew", padx=1, pady=1)
        ch_title2 = Label(ch_frame, text='Max Value', bg='white', borderwidth=0, width=0)
        ch_title2.grid(row=0, column=column+1, sticky="nsew", padx=1, pady=1)
        for i in i:
            self.box = Entry(ch_frame, bg='white', borderwidth=0, width=0)
            self.box.grid(row=1, column=column+i, sticky="nsew", padx=1, pady=1)

def StartBut(self):
    value = self.box.get()

but1 = Button(frame_but, text='Start', command =StartBut(self))

UPDATE: In fact what I would like to do is return the box values without having to use classes or self if possible? I’m afraid that introducing classes and self etc now into my code would take too much time to rewrite it all..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:37:49+00:00Added an answer on June 6, 2026 at 7:37 am

    Ok, just realised from reading comments that the Button class here is from tkinter.
    This changes the nature of the question a little.

    The command argument of the Button constructor is a callable with no arguments.
    This means that if you just pass in a function, you can’t actually pass any arguments to that function.
    So if you want that function to have access to the but1 instance, then you will need to do something else.

    One thing is to wrap the whole thing in a class, eg:

    class MyButton(object):
      def __init__(self, frame_but, text):
        self.but = Button(frame_but, text, command=self.StartBut)
    
      def StartBut(self):
        value = self.but.box.get()
    

    This will all depend on what you’re trying to achieve, and how you are designing things.

    Also, I’m not sure what the ChBox function is trying to achieve.

    Old answer

    In python, “self” is usually the argument name used for class methods.

    For example, if you have the following:

    class Foo(object):
      def echo(self, a):
        return a
    

    then when you write:

    f = Foo()
    f.echo(2)
    

    the call to f.echo(2), it gets translated into something like Foo.echo(f, 2), so the instance f becomes the first argument, and hence is bound to self, and 2 is the second argument, bound to a.
    In fact, you could actually call Foo.echo(f, 2) and it will do the exact same thing as f.echo(2).

    I’m not sure what your Button class is doing, but supposing it does something like:

    class Button(object):
      ...
      def hit(self):
        self.command()
    

    And what you want to achieve is to pass the current Button instance to the command, you should write instead:

    class Button(object):
      ...
      def hit(self):
        self.command(self)
    

    and when constructing an instance:

    but1 = Button(frame_but, text='Start', command =StartBut)
    

    You probably also want to rename the argument of StartBut, as self is normally only used as the first argument of methods:

    def StartBut(button):
      value = button.box.get()
    

    Sidenote

    Understanding that instance.method(args) translates to Class.method(instance, args) will go a long way to helping you understand a lot of python semantics.

    For instance, when learning to join a list into a string in python, you often see ' '.join(["hello", "world"]).
    A lot of novices find this confusing and complain about the inconsistent syntax of python.
    But really, this is the hugely consistent with the rest of python — all it is doing is calling str.join(' ', ["hello", "world"]), and this should look more like the familiar form of join found in other languages. ' '.join(["hello", "world"]) is just a pythonic way of calling this method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.