I've been working on a tic-tac-toe projejct, and I am planning on creating 9 button widgets with a picture on each one of them. I want to move them so they'll be 3 on a line and 3 in a row. Here's my code:
#imports:
from Tkinter import *
from PIL import ImageTk, Image
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#constants
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#classes:
class App(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
myapp = App()
class GUI:
def __init__(self):
self.myapp = myapp
self.myapp.master.title("tkname")
self.myapp.master.maxsize(2000, 1200)
def create_and_pack_canvas(self, game_board_height, game_board_width):
canvas = Canvas(height = game_board_height, width = game_board_width)
canvas.pack(expand=1, fill=BOTH)
return canvas
def form_game_board(self):
x = 404
y = 150
canvas = self.create_and_pack_canvas(1000, 1000)
for i in range(3):
for j in range(3):
btn = gameButton("")
btn.create_and_pack(canvas, x, y)
x += 200
x = 404
y += 208
self.myapp.mainloop()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class gameButton:
def __init__(self, picture_param):
self.picture = ImageTk.PhotoImage(Image.open("somepic"))
self.picture_height = self.picture.height()
self.picture_width = self.picture.width()
def callback(self):
print 10
def create_and_pack(self, canvas, x_pixels, y_pixels):
self.b = Button(myapp, text="Print 10~", command = self.callback, image = self.picture)
self.b.pack()
#self.b.place(bordermode = OUTSIDE, x = x_pixels, y = y_pixels)
def __str__(self):
pass
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#main
def main():
gui = GUI()
gui.form_game_board()
if __name__ == "__main__":
main()
Please note that there's this line:
#self.b.place(bordermode = OUTSIDE, x = x_pixels, y = y_pixels)
In the code, which I've put as a note, which is basically the line that's supposed to move the buttons. If you run the code, it'll simply put all the buttons one under another in a row, 9 of them. After the self.b.place() line, they all simply disappear. Maybe they moved somewhere too far, maybe not, but I assume the place() isn't working, and I've had trouble figuring why.
Thanks in advance.
canvasparameter ingameButton.create_and_packis not used. I strongly recommend using a explicitroot = Tk().