Tkinter : Button in frame not visible
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 tkclass 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", nroot = tk.Tk()Buttons(root)root.mainloop()
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.