m s m s - 5 months ago 51
Python Question

Tkinter : Button in frame not visible

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()

Answer

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()