Python Tkinter keyboard using bind -
i have problem using bind option can click button in keyboard, , call function once key pressed. tried taking other codes have similar purpose , saw i'm doing pretty same, though still have problem must've missed. thankful if me this. here's code:
# -*- coding: utf-8 -*- #imports tkinter import * pil import imagetk, image import time import tkmessagebox #===========================================================================================================================================# #tkinter class class app(frame): def __init__(self, master=none): frame.__init__(self, master) self.pack() #===========================================================================================================================================# #game pieces classes class board: def __init__(self, rows = 7, columns = 6, picture_height = none, picture_width = none): self.rows = rows self.columns = columns self.picture = imagetk.photoimage(image.open(r"c:\users\ariel\desktop\python\4inarow.gif")) self.picture_height = self.picture.height() self.picture_width = self.picture.width() def create_and_pack(self, canvas): board_item = canvas.create_image(700, 370, image = self.picture) def __str__(self): print "rows:", self.rows, "columns:", self.columns class disk: def __init__(self, player_number = none): self.picture = imagetk.photoimage(image.open(r"c:\users\ariel\desktop\python\me" + str(player_number) + ".gif")) self.player_number = player_number def create_and_pack(self, canvas, x, y): disk_item = canvas.create_image(x, y, image = self.picture) def get_x_parameter(self, number): #x growing 70~73 per number x = 330 in range(7): if number == i: x = x + * 72 return x def get_y_parameter(self, number): #y lowered 70~73 per number y = 635 in range(6): if number == i: y = y - * 72 return y def place(self, canvas, x, y): #first left down circle parameters #480, 635 canvas.move(self.picture, x, y) def __str__(self): print "the disk's picture string:", self.picture, "the player disk's number:", self.player_number #===========================================================================================================================================# #game class class game: def __init__(self, board = none, disk1 = none, disk2 = none): self.disk1 = disk1 self.disk2 = disk2 self.board = board #===========================================================================================================================================# #keyboardclass class keyboard: def __init__(self, key_number = none): self.key_number = key_number def press_and_place(self, canvas, number, function): canvas.focus_set() canvas.bind("<" + str(number) + ">", function) #===========================================================================================================================================# #main. myapp = app() myapp.master.title("4 in row") myapp.master.maxsize(2000, 1200) #---------------------------------------------------------------------------------------------------# gameboard = board(7, 6) firstplayerdisk = disk(1) secondplayerdisk = disk(2) gameclass = game(gameboard, firstplayerdisk, secondplayerdisk) #---------------------------------------------------------------------------------------------------# #creating canvas , placing disks , board. board_canvas = canvas(width = gameboard.picture_width, height = gameboard.picture_height) board_canvas.pack(expand=1, fill=both) gameboard.create_and_pack(board_canvas) firstplayerdisk.create_and_pack(board_canvas, 330, 635) secondplayerdisk.create_and_pack(board_canvas, 260, 635) #---------------------------------------------------------------------------------------------------# #creating keyboard instance , placing first disk in last row in column of choice number_choice = 3 keyboardclass = keyboard(number_choice) first_player_x = firstplayerdisk.get_x_parameter(number_choice) first_player_y = firstplayerdisk.get_y_parameter(number_choice) keyboardclass.press_and_place(board_canvas, number_choice, firstplayerdisk.place(board_canvas, first_player_x, first_player_y)) #---------------------------------------------------------------------------------------------------# myapp.mainloop()
thanks lot in advance.
i believe problem lies in line:
keyboardclass.press_and_place(board_canvas, number_choice, firstplayerdisk.place(board_canvas, first_player_x, first_player_y))
the third argument firstplayerdisk.place(board_canvas, first_player_x, first_player_y)
none
type
def place(self, canvas, x, y): #first left down circle parameters #480, 635 canvas.move(self.picture, x, y)
returns none
from how bind keypress button in tkinter , this site, need pass function, is, firstplayerdisk.place
(no parenthesis following it).
Comments
Post a Comment