Re: Question:Programming a game grid ...

Discussion in 'Python' started by David, Jun 28, 2012.

  1. David

    David Guest

    First, you should be getting an error on
    vars()[var] = Button(f3, text = "00", bg = "white")
    as vars() has not been declared and it does not appear to be valid Python syntax. I don't see a reason to store a reference to the button since you won't be modifying them. Also, you can not mix pack() and grid(). It produces unpredictable results.

    try:
    import Tkinter as tk ## Python 2.x
    except ImportError:
    import tkinter as tk ## Python 3.x

    def leftclick(*args):
    print "leftclick called"

    def rightclick(*args):
    print "rightclick called"

    root = tk.Tk()

    f3 = tk.Frame(root, bg = "white", width = 500)
    f3.grid()

    this_row=0
    this_column=0
    for ctr in range(0, 89):
    b = tk.Button(f3, text = "%0d" % (ctr), bg = "white")
    b.grid(row=this_row, column=this_column)
    b.bind('<Button-1>', leftclick) # bind left mouse click
    b.bind('<Button-3>', rightclick) # bind left mouse click
    this_column += 1
    if this_column > 6:
    this_column=0
    this_row += 1
    root.title('Puzzle Grid')
    root.mainloop()
    David, Jun 28, 2012
    #1
    1. Advertising

  2. On Thu, Jun 28, 2012 at 9:24 AM, David <> wrote:
    > First, you should be getting an error on
    > vars()[var] = Button(f3, text = "00", bg = "white")
    > as vars() has not been declared and it does not appear to be valid Python syntax.


    It's valid syntax, but highly inadvisable. What it does is call the
    vars() function, then dereference its argument for assignment -
    perfectly legal when the function returns a dictionary, which vars
    does. But check the docs:

    http://docs.python.org/library/functions.html#vars

    It's intended to be read, NOT written. It's entirely possible that
    this works at module level, but should not be relied on. Also, other
    Python implementations or even other versions of the same Python could
    behave differently.

    ChrisA
    Chris Angelico, Jun 28, 2012
    #2
    1. Advertising

  3. On Thu, Jun 28, 2012 at 9:35 AM, Chris Angelico <> wrote:
    > On Thu, Jun 28, 2012 at 9:24 AM, David <> wrote:
    >> First, you should be getting an error on
    >> vars()[var] = Button(f3, text = "00", bg = "white")
    >> as vars() has not been declared and it does not appear to be valid Python syntax.

    >
    > It's valid syntax, but highly inadvisable.


    Clarification: There's nothing inadvisable about the syntax, just
    about writing to vars(). Dereferencing a function's return value is
    perfectly alright.

    ChrisA
    Chris Angelico, Jun 28, 2012
    #3
  4. On Wed, 27 Jun 2012 16:24:30 -0700, David wrote:

    > First, you should be getting an error on
    > vars()[var] = Button(f3, text = "00", bg = "white")
    > as vars() has not been declared


    The Fine Manual says differently:

    Python 2:
    http://docs.python.org/library/functions.html#vars

    Python 3:
    http://docs.python.org/py3k/library/functions.html#vars


    > and it does not appear to be valid Python syntax.


    It's perfectly fine syntax, no different from:

    my_dict['spam'] = 'a yummy ham-like substance'

    or similar.


    --
    Steven
    Steven D'Aprano, Jun 28, 2012
    #4
  5. David

    Guest

    On Wednesday, June 27, 2012 5:15:09 PM UTC-7, Steven D'Aprano wrote:
    > On Wed, 27 Jun 2012 16:24:30 -0700, David wrote:
    >
    > > First, you should be getting an error on
    > > vars()[var] = Button(f3, text = "00", bg = "white")
    > > as vars() has not been declared

    >
    > The Fine Manual says differently:
    >
    > Python 2:
    > http://docs.python.org/library/functions.html#vars
    >
    > Python 3:
    > http://docs.python.org/py3k/library/functions.html#vars
    >
    >
    > > and it does not appear to be valid Python syntax.

    >
    > It's perfectly fine syntax, no different from:
    >
    > my_dict['spam'] = 'a yummy ham-like substance'
    >
    > or similar.
    >
    >
    > --
    > Steven


    "as vars() has not been declared and it does not appear to be valid Python syntax"

    You assume too much IMHO. Vars() was not declared in the code provided and I do not think that we should be assuming that it is a function returning a dictionary instead of an error. Just my opinion.
    , Jun 28, 2012
    #5
  6. David

    alex23 Guest

    On Jun 28, 12:15 pm, wrote:
    > You assume too much IMHO.  Vars() was not declared in
    > the code provided and I do not think that we should be
    > assuming that it is a function returning a dictionary instead
    > of an error.


    http://docs.python.org/library/functions.html#vars

    Do you have the same objection to every built-in?

    > Just my opinion.


    There are two ways to help people: by trying to understand what
    they're doing, or by submitting them to endless pedantry. Only one of
    those is actually helpful.
    alex23, Jun 28, 2012
    #6
  7. On Wed, 27 Jun 2012 19:43:07 -0700 (PDT), alex23 <>
    wrote:

    >There are two ways to help people: by trying to understand what
    >they're doing, or by submitting them to endless pedantry. Only one of
    >those is actually helpful.


    Is it alright if I use that as a quote? Properly attributed, of
    course.
    --
    The amazing programming device: fuelled entirely by coffee, it codes while
    awake and tests while asleep!
    Temia Eszteri, Jun 28, 2012
    #7
  8. On Wed, 27 Jun 2012 19:15:54 -0700, woooee wrote:

    > "as vars() has not been declared and it does not appear to be valid
    > Python syntax"
    >
    > You assume too much IMHO. Vars() was not declared in the code provided
    > and I do not think that we should be assuming that it is a function
    > returning a dictionary instead of an error. Just my opinion.


    Thanks for sharing.

    It's an ignorant opinion. vars is a built-in function, just like len, chr,
    map, and many others.

    Ignorance is not a sin, but willful ignorance is. I already gave the
    links showing that vars is a built-in function, and you chose to ignore
    them. Here they are again. Please take a few seconds to read at least one
    of them, so that you will be a better programmer in the future and your
    opinion will be less ignorant:

    Python 2:
    http://docs.python.org/library/functions.html#vars

    Python 3:
    http://docs.python.org/py3k/library/functions.html#vars



    --
    Steven
    Steven D'Aprano, Jun 28, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BlackHawke
    Replies:
    12
    Views:
    1,549
    Andrew Thompson
    Jan 26, 2004
  2. Max Kubierschky
    Replies:
    10
    Views:
    1,827
    pabloreda
    Mar 31, 2007
  3. MRAB
    Replies:
    0
    Views:
    174
  4. alex23
    Replies:
    0
    Views:
    194
    alex23
    Jun 28, 2012
  5. Rick Johnson

    Re: Question:Programming a game grid ...

    Rick Johnson, Jun 28, 2012, in forum: Python
    Replies:
    0
    Views:
    201
    Rick Johnson
    Jun 28, 2012
Loading...

Share This Page