Tkinter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay

    #1

    Tkinter

    I wold like to be able to generate buttons from a list in a file.
    How would I go about making each button have a different command,
    Lets say make the button print its label instead of print "."

    The problem I have is I don't know what the list is going to be until
    it is opened or how big so I carnet make a def for each.

    And I don't know how to do it with a loop

    Thanks

    ------Code-------

    from Tkinter import *

    class App:
    def __init__(self, root):

    self.DisplayAre aListsFrame = Frame(root)
    self.DisplayAre aListsFrame.pac k()

    Lists = ["A1","B2","C3", "D4"]

    for i in Lists:
    self.DisplayAre aListButton = Button(
    self.DisplayAre aListsFrame,
    text=i,
    command=self.Li stButtonCommand )
    self.DisplayAre aListButton.pac k(side=LEFT,pad x=10)

    def ListButtonComma nd(self):
    print "."

    root = Tk()
    app = App(root)
    root.mainloop()

  • Fredrik Lundh

    #2
    Re: Tkinter

    "Jay" wrote:
    [color=blue]
    > I wold like to be able to generate buttons from a list in a file.
    > How would I go about making each button have a different command,
    > Lets say make the button print its label instead of print "."
    >
    > The problem I have is I don't know what the list is going to be until
    > it is opened or how big so I carnet make a def for each.[/color]

    def is an executable statement, and creates a new function object
    everytime it's executed. if you want a new function for each pass
    through a loop, just put the def inside the loop:
    [color=blue]
    > from Tkinter import *
    >
    > class App:
    > def __init__(self, root):
    >
    > self.DisplayAre aListsFrame = Frame(root)
    > self.DisplayAre aListsFrame.pac k()
    >
    > Lists = ["A1","B2","C3", "D4"]
    >
    > for i in Lists:[/color]

    def callback(text=i ):
    print text
    [color=blue]
    > self.DisplayAre aListButton = Button(
    > self.DisplayAre aListsFrame,
    > text=i,
    > command=callbac k)
    > self.DisplayAre aListButton.pac k(side=LEFT,pad x=10)
    >
    > root = Tk()
    > app = App(root)
    > root.mainloop()[/color]

    the only think that's a little bit tricky is scoping: you can access all
    variables from the __init__ method's scope from inside the callbacks,
    but the callbacks will be called after the loop is finished, so the "i"
    variable will be set to the *last* list item for all callbacks.

    to work around this, you have to explicitly pass the *value* of "i" to
    to the callback; the "text=i" part in the above example uses Python's
    default argument handling to do exactly that.

    </F>



    Comment

    • Jay

      #3
      Re: Tkinter

      I still dont quite get it, is their sum kind of way you can [def Newdef
      + i()] for example so that the def is different from the string, all
      the buttons now have numbers on

      If I am not mistaken Fredrik Lundh rote the tutorial Learning to
      program (Python) www.freenetpages.co.uk/hp/alan.gauld/ and


      If this is the same person then thank you for everything I no about
      python it seams what ever I try and learn about Python it is always you
      who helps or has wrote docs to help Newbie's and well everyone
      really.

      ---code---
      from Tkinter import *

      class App:
      def __init__(self, root):

      self.DisplayAre aListsFrame = Frame(root)
      self.DisplayAre aListsFrame.pac k()

      Lists = ["A1","B2","C3", "D4"]
      for i in Lists:
      def i():
      print i
      self.DisplayAre aListButton = Button(
      self.DisplayAre aListsFrame,
      text=i,
      command=i)
      self.DisplayAre aListButton.pac k(side=LEFT,pad x=10)



      root = Tk()
      app = App(root)
      root.mainloop()

      Comment

      • Ravi Teja

        #4
        Re: Tkinter

        Documentation isn't his only contribution, BTW. He wrote a whole bunch
        of highly useful modules for Python as well.


        Comment

        Working...