2

I'm trying to implement a TicTacToe program. I am an absolute beginner in python. After viewing many tutorials and reading a few books, I have understood the basics of Python. I'm trying to get the buttons to display in a frame, but all I get is a blank window.

link for image of the resultant window

This is the code I have so far:

from Tkinter import *

class Buttons(object):

    def __init__(self,master):
        frame = Frame(master)
        frame.pack()

        self.button1= Button(frame,text="1",height=4,width=8,command=self.move)
        self.button1.pack(side=LEFT)

        self.button2= Button(frame,text="2",height=4,width=8,command=self.move)
        self.button2.pack(side=LEFT)

        self.button3= Button(frame,text="3",height=4,width=8,command=self.move)
        self.button3.pack(side=LEFT)


root = Tk()
root=mainloop()
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
m s
  • 23
  • 5
  • Okay,I understood the problem,needed to put b=Buttons(root) for it to work. It would be helpful if explained why this was required? Thank you. – m s Jun 19 '16 at 05:49
  • Ok, I'll post an answer in a couple of minutes. – PM 2Ring Jun 19 '16 at 05:50

2 Answers2

3

You defined your Buttons class but you didn't create an instance of that class, so no buttons were actually constructed. Also, you had a typo / syntax error:

root=mainloop()

should be

root.mainloop()

Also, you didn't define the move callback method.

Here's a repaired version of your code:

from Tkinter import *

class Buttons(object):

    def __init__(self,master):
        frame = Frame(master)
        frame.pack()

        self.button1 = Button(frame, text="1", height=4, width=8, command=self.move)
        self.button1.pack(side=LEFT)

        self.button2 = Button(frame, text="2", height=4, width=8, command=self.move)
        self.button2.pack(side=LEFT)

        self.button3 = Button(frame, text="3", height=4, width=8, command=self.move)
        self.button3.pack(side=LEFT)

    def move(self):
        print "click!"

root = Tk()
Buttons(root)
root.mainloop()

However, this still has a problem: The move method has no way of knowing which button called it. Here's one way to fix that. I've also changed

from Tkinter import *

to

import tkinter as tk

It's not a good idea to use "star" imports. They make code harder to read and they pollute your namespace with all the names defined in the imported module (that's 175 names in the case of Tkinter), which can lead to name collisions.

import Tkinter as tk

class Buttons(object):

    def __init__(self,master):
        frame = tk.Frame(master)
        frame.pack()

        self.buttons = []
        for i in range(1, 4):
            button = tk.Button(
                frame, text=i, height=4, width=8, 
                command=lambda n=i:self.move(n)
            )
            button.pack(side=tk.LEFT)
            self.buttons.append(button)

    def move(self, n):
        print "click", n

root = tk.Tk()
Buttons(root)
root.mainloop()
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
  • thank you so much for this post! It is immensely helpful! – m s Jun 19 '16 at 06:00
  • @ms: My pleasure! Good luck with your program. You will probably find it easier to build your tic-tac-toe grid using the `.grid` method instead of `.pack`. – PM 2Ring Jun 19 '16 at 06:06
  • yes,I am on to that already. I am pretty sure to face a lot of bumps once i start implementing the gaming portion. Hopefully SO will help me through it :D thanks once again! – m s Jun 19 '16 at 06:10
0

Okay the problem was I needed to add a variable at the end of the code. Something like b=Buttons(root). It's working now.

m s
  • 23
  • 5