Python Forum

Full Version: How do you create buttons and what is or how does super work?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Currently trying to create buttons while using Tkinter but not quite sure. How to create them and
what exactly does super do?
Because from what I read of it just calls upon class for use.
Little help and explaining would be most useful.
Here's an example with 3 buttons:

import tkinter as tk
import sys

class TryButtons(tk.Frame):
    def __init__(self, title='Try Buttons', parent=None):
        self.has_parent = False
        if parent is None:
            self.parent = tk.Tk()
        else:
            self.parent = parent
            self.has_parent = True
        self.parent.geometry('160x60+10+10')
        tk.Frame.__init__(self, self.parent)
        self.grid()

        self.add_some_buttons()

    def add_some_buttons(self):
        self.button1 = tk.Button(self.parent, text='Left')
        self.button2 = tk.Button(self.parent, text='Middle')
        self.button3 = tk.Button(self.parent, text='Right')
        self.quit = tk.Button(self.parent, text='QUIT')

        self.button1.grid(row=0, column=0, padx=2, pady=2, sticky='nsew')
        self.button2.grid(row=0, column=1, padx=2, pady=2, sticky='nsew')
        self.button3.grid(row=0, column=2, padx=2, pady=2, sticky='nsew')
        self.quit.grid(row=1, column=1, padx=2, pady=2, sticky='nsew')

        self.button1.bind('<Button-1>', self.button_pressed)
        self.button2.bind('<Button-1>', self.button_pressed)
        self.button3.bind('<Button-1>', self.button_pressed)
        self.quit.bind('<Button-1>', self.button_pressed)

        if not self.has_parent:
            self.mainloop()

    def button_pressed(self, event):
        text = event.widget.cget("text")
        print(text)
        if text == 'QUIT':
            self.parent.destroy()
            sys.exit()


def testit():
    TryButtons(title='No parent')

if __name__ == '__main__':
    testit()
the class can be instantiated with or without a parent.
In this example, (line 39) I instantiate without
  • lines 6 - 11: Check to see if parent has been passed as argument
  • if No: (line 8) create a root window as parent
  • if yes: (lines 10, 11) use it and set flag which will be used to create mainloop if necessary
  • Line 12 - Set window to be 160 x 60 pixels indented 10 on both x and y
  • Line 13 - Initialize the tkinter Frame (inherited)
  • Line 14 - Call grid to position Frame
  • Line 16 - Add the buttons
  • Lines 19-22 - create button widgets
  • Lines 24-27 - Set button positions on Frame (self.parent)
  • Lines 29-32 - Bind buttons to same method (you can use separate methods for each if you wish
  • Lines 34-35 - add mainloop is parent created here.
  • Lines 37-39 - Determine which button was pressed (event.widget) and print text value
  • Lines 40-42 - Quit application if quit button pressed
Hello, what I am going to ask will probably help the author, creating a new conversation for the same kind of question would be useless I think. Could you please me?

My question is: I created a simplified Go game on Python where the board is represented by a 6x6 matrix and your stones by "1" and the enemy's by "-1" in it. The rule is simple, circle the enemy, the first one to lose a stone/group of stones loses the game.
I wanted to create a graphic interface with Tkinter but I know very few about this despite watching some tutorials which never helped me for what I want to know. I put 25 buttons on the graphic interface (for a 5x5 game, not 6x6 but there is a reason) and when I click on one of those, I would like a circle representing my stone to appear, while the button disappears, plus, if possible, a "1" to appear in the matrix (corresponding of course at the same coordinates). That will be more annoying with the enemy's turn for it's the computer.
That must sound easy for you but I don't know how to do it.

Thank you helping me.