0

I'm learning to use tkinter in Python 3.6.4. I am creating a GUI with multiple instances of buttons. Two such instances are:

def createWidgets(self):
    # first button
    self.QUIT = Button(self)
    self.QUIT["text"] = "Quit"
    self.QUIT["command"] = self.quit
    self.QUIT.pack()

    # second button
    self.Reset  = Button(self)
    self.Reset["text"] = "Reset"
    self.Reset["command"] = "some other function, tbd"

What I want to learn is how to abstract the instantiation of buttons such that each instance in the createWidgets method is based on a method something like this:

createButton( self, text, command, fg, bg, hgt, wth, cursor ):

What I don't know is how to control the naming of the button as:

self.QUIT
self.Reset

where the property or name following the "." operator can be passed to the createButton as a property by which the button is created and named.

4
  • 2
    use a dictionary. self.widget["QUIT"] Commented Mar 9, 2018 at 13:49
  • Something like this? def createButton( self, text):, followed by self.widget( text ) = Button( self), where text can be "QUIT" or other string? Commented Mar 9, 2018 at 13:58
  • It that is correct, then in the main method, createWidgets, the use of createButton is used how, please? Commented Mar 9, 2018 at 14:09
  • Benjamin, I noticed you have not accepted any answer for any of the question you have asked. Please take the time to select the check box next to the answer that helped you solve your problem. Commented Mar 9, 2018 at 20:10

1 Answer 1

3

Simply expanding on what Brian said, this code will get you going. The button objects are stored in a widget dictionary. Here is one way to put this together:

import tkinter as tk
import sys

root = tk.Tk()

class CustomButton(tk.Button):

    def __init__(self, parent, **kwargs):
        tk.Button.__init__(self, parent)
        for attribute,value in kwargs.items():
            try:
                self[attribute] = value
            except:
                raise

def doReset():
    print("doRest not yet implemented")

if __name__ == '__main__':
    widget = {}
    widget['quit'] = CustomButton(root, text='Quit', command=sys.exit)
    widget['reset'] = CustomButton(root, text='Reset', command=doReset)
    for button in widget.keys():
        widget[button].pack()
    root.mainloop()
Sign up to request clarification or add additional context in comments.

5 Comments

Got it. Much obliged.
@BenjaminLevy You can select a single answer if it was the most helpful one to you.
Does this help?
@BenjaminLevy I'm not sure what you mean by "this" but there's upvote and there's answer select, the answer above isn't selected, but it was upvoted as of now 3 times.
Sorry, Nae. Forgot to click on the 'check' icon.

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.