Python Forum
Chess on Tkinter Snapping Issues
Thread Rating:
  • 4 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Chess on Tkinter Snapping Issues
#1
I'm working on a python chess game on tkinter, but I'm having trouble with moving the pieces. I've created the board, I placed a letter/number combination on each square (F3, A7, etc) but now I'm not sure how to move the pieces. I haven't created the pieces yet either.
Here's my code so far:

""" Python Chess Made by Trinx """
import time
print('Python Chess')
print('Your game of chess will now begin.')
time.sleep(1.8)
from tkinter import *
size = 600
window = Tk()
window.title('Python Chess')
canvas = Canvas(window, width=size, height=size)
canvas.pack()
#Horizontal lines on the board
lineh1 = canvas.create_line(75, 0, 75, 600)
lineh2 = canvas.create_line(150, 0, 150, 600)
lineh3 = canvas.create_line(225, 0, 225, 600)
lineh4 = canvas.create_line(300, 0, 300, 600)
lineh5 = canvas.create_line(375, 0, 375, 600)
lineh6 = canvas.create_line(450, 0, 450, 600)
lineh7 = canvas.create_line(525, 0, 525, 600)
lineh8 = canvas.create_line(600, 0, 600, 600)
lineh9 = canvas.create_line(0, 0, 600, 0)

#Vertical lines on the board
linev1 = canvas.create_line(0, 75, 600, 75)
linev2 = canvas.create_line(0, 150, 600, 150)
linev3 = canvas.create_line(0, 225, 600, 225)
linev4 = canvas.create_line(0, 300, 600, 300)
linev5 = canvas.create_line(0, 375, 600, 375)
linev6 = canvas.create_line(0, 450, 600, 450)
linev7 = canvas.create_line(0, 525, 600, 525)
linev8 = canvas.create_line(0, 600, 600, 600)
linev9 = canvas.create_line(0, 600, 0, 0)

#Grey squares:
square1 = canvas.create_rectangle(75, 0, 150, 75, fill='grey')
square1 = canvas.create_rectangle(225, 0, 300, 75, fill='grey')
square1 = canvas.create_rectangle(375, 0, 450, 75, fill='grey')
square1 = canvas.create_rectangle(525, 0, 600, 75, fill='grey')

square1 = canvas.create_rectangle(0, 75, 75, 150, fill='grey')
square1 = canvas.create_rectangle(150, 75, 225, 150, fill='grey')
square1 = canvas.create_rectangle(300, 75, 375, 150, fill='grey')
square1 = canvas.create_rectangle(450, 75, 525, 150, fill='grey')

square1 = canvas.create_rectangle(75, 150, 150, 225, fill='grey')
square1 = canvas.create_rectangle(225, 150, 300, 225, fill='grey')
square1 = canvas.create_rectangle(375, 150, 450, 225, fill='grey')
square1 = canvas.create_rectangle(525, 150, 600, 225, fill='grey')

square1 = canvas.create_rectangle(0, 225, 75, 300, fill='grey')
square1 = canvas.create_rectangle(150, 225, 225, 300, fill='grey')
square1 = canvas.create_rectangle(300, 225, 375, 300, fill='grey')
square1 = canvas.create_rectangle(450, 225, 525, 300, fill='grey')

square1 = canvas.create_rectangle(75, 300, 150, 375, fill='grey')
square1 = canvas.create_rectangle(225, 300, 300, 375, fill='grey')
square1 = canvas.create_rectangle(375, 300, 450, 375, fill='grey')
square1 = canvas.create_rectangle(525, 300, 600, 375, fill='grey')

square1 = canvas.create_rectangle(0, 375, 75, 450, fill='grey')
square1 = canvas.create_rectangle(150, 375, 225, 450, fill='grey')
square1 = canvas.create_rectangle(300, 375, 375, 450, fill='grey')
square1 = canvas.create_rectangle(450, 375, 525, 450, fill='grey')

square1 = canvas.create_rectangle(75, 450, 150, 525, fill='grey')
square1 = canvas.create_rectangle(225, 450, 300, 525, fill='grey')
square1 = canvas.create_rectangle(375, 450, 450, 525, fill='grey')
square1 = canvas.create_rectangle(525, 450, 600, 525, fill='grey')

square1 = canvas.create_rectangle(0, 525, 75, 600, fill='grey')
square1 = canvas.create_rectangle(150, 525, 225, 600, fill='grey')
square1 = canvas.create_rectangle(300, 525, 375, 600, fill='grey')
square1 = canvas.create_rectangle(450, 525, 525, 600, fill='grey')

#1st vertical line moves:
canvas.create_text(37, 65, text='A1', fill='black')
canvas.create_text(37, 140, text='B1', fill='white')
canvas.create_text(37, 215, text='C1', fill='black')
canvas.create_text(37, 290, text='D1', fill='white')
canvas.create_text(37, 365, text='E1', fill='black')
canvas.create_text(37, 440, text='F1', fill='white')
canvas.create_text(37, 515, text='G1', fill='black')
canvas.create_text(37, 590, text='H1', fill='white')

#2nd vertical line moves:
canvas.create_text(112, 65, text='A2', fill='white')
canvas.create_text(112, 140, text='B2', fill='black')
canvas.create_text(112, 215, text='C2', fill='white')
canvas.create_text(112, 290, text='D2', fill='black')
canvas.create_text(112, 365, text='E2', fill='white')
canvas.create_text(112, 440, text='F2', fill='black')
canvas.create_text(112, 515, text='G2', fill='white')
canvas.create_text(112, 590, text='H2', fill='black')

#3rd vertical line moves:
canvas.create_text(187, 65, text='A3', fill='black')
canvas.create_text(187, 140, text='B3', fill='white')
canvas.create_text(187, 215, text='C3', fill='black')
canvas.create_text(187, 290, text='D3', fill='white')
canvas.create_text(187, 365, text='E3', fill='black')
canvas.create_text(187, 440, text='F3', fill='white')
canvas.create_text(187, 515, text='G3', fill='black')
canvas.create_text(187, 590, text='H3', fill='white')

#4th vertical line moves:
canvas.create_text(262, 65, text='A4', fill='white')
canvas.create_text(262, 140, text='B4', fill='black')
canvas.create_text(262, 215, text='C4', fill='white')
canvas.create_text(262, 290, text='D4', fill='black')
canvas.create_text(262, 365, text='E4', fill='white')
canvas.create_text(262, 440, text='F4', fill='black')
canvas.create_text(262, 515, text='G4', fill='white')
canvas.create_text(262, 590, text='H4', fill='black')

#5th vertical line moves:
a5 = canvas.create_text(337, 65, text='A5', fill='black')
canvas.create_text(337, 140, text='B5', fill='white')
canvas.create_text(337, 215, text='C5', fill='black')
canvas.create_text(337, 290, text='D5', fill='white')
canvas.create_text(337, 365, text='E5', fill='black')
canvas.create_text(337, 440, text='F5', fill='white')
canvas.create_text(337, 515, text='G5', fill='black')
canvas.create_text(337, 590, text='H5', fill='white')

#6th vertical line moves:
canvas.create_text(412, 65, text='A6', fill='white')
canvas.create_text(412, 140, text='B6', fill='black')
canvas.create_text(412, 215, text='C6', fill='white')
canvas.create_text(412, 290, text='D6', fill='black')
canvas.create_text(412, 365, text='E6', fill='white')
canvas.create_text(412, 440, text='F6', fill='black')
canvas.create_text(412, 515, text='G6', fill='white')
canvas.create_text(412, 590, text='H6', fill='black')

#7th vertical line moves:
canvas.create_text(487, 65, text='A7', fill='black')
canvas.create_text(487, 140, text='B7', fill='white')
canvas.create_text(487, 215, text='C7', fill='black')
canvas.create_text(487, 290, text='D7', fill='white')
canvas.create_text(487, 365, text='E7', fill='black')
canvas.create_text(487, 440, text='F7', fill='white')
canvas.create_text(487, 515, text='G7', fill='black')
canvas.create_text(487, 590, text='H7', fill='white')

#8th vertical line moves:
canvas.create_text(562, 65, text='A8', fill='white')
canvas.create_text(562, 140, text='B8', fill='black')
canvas.create_text(562, 215, text='C8', fill='white')
canvas.create_text(562, 290, text='D8', fill='black')
canvas.create_text(562, 365, text='E8', fill='white')
canvas.create_text(562, 440, text='F8', fill='black')
canvas.create_text(562, 515, text='G8', fill='white')
canvas.create_text(562, 590, text='H8', fill='black')

#Buttons:

from tkinter import *
window = Tk()
window.title('Chess Controller')
commands = Canvas(window, width=300, height=200)
commands.pack()
A1 = Button(window, text='A1')
A2 = Button(window, text='A2')
A3 = Button(window, text='A3')
A4 = Button(window, text='A4')
A5 = Button(window, text='A5')
A6 = Button(window, text='A6')
A7 = Button(window, text='A7')
A8 = Button(window, text='A8')

B1 = Button(window, text='B1')
B2 = Button(window, text='B2')
B3 = Button(window, text='B3')
B4 = Button(window, text='B4')
B5 = Button(window, text='B5')
B6 = Button(window, text='B6')
B7 = Button(window, text='B7')
B8 = Button(window, text='B8')

C1 = Button(window, text='C1')
C2 = Button(window, text='C2')
C3 = Button(window, text='C3')
C4 = Button(window, text='C4')
C5 = Button(window, text='C5')
C6 = Button(window, text='C6')
C7 = Button(window, text='C7')
C8 = Button(window, text='C8')

D1 = Button(window, text='D1')
D2 = Button(window, text='D2')
D3 = Button(window, text='D3')
D4 = Button(window, text='D4')
D5 = Button(window, text='D5')
D6 = Button(window, text='D6')
D7 = Button(window, text='D7')
D8 = Button(window, text='D8')

E1 = Button(window, text='E1')
E2 = Button(window, text='E2')
E3 = Button(window, text='E3')
E4 = Button(window, text='E4')
E5 = Button(window, text='E5')
E6 = Button(window, text='E6')
E7 = Button(window, text='E7')
E8 = Button(window, text='E8')

F1 = Button(window, text='F1')
F2 = Button(window, text='F2')
F3 = Button(window, text='F3')
F4 = Button(window, text='F4')
F5 = Button(window, text='F5')
F6 = Button(window, text='F6')
F7 = Button(window, text='F7')
F8 = Button(window, text='F8')

G1 = Button(window, text='G1')
G2 = Button(window, text='G2')
G3 = Button(window, text='G3')
G4 = Button(window, text='G4')
G5 = Button(window, text='G5')
G6 = Button(window, text='G6')
G7 = Button(window, text='G7')
G8 = Button(window, text='G8')

H1 = Button(window, text='H1')
H2 = Button(window, text='H2')
H3 = Button(window, text='H3')
H4 = Button(window, text='H4')
H5 = Button(window, text='H5')
H6 = Button(window, text='H6')
H7 = Button(window, text='H7')
H8 = Button(window, text='H8')

A1.pack()
A2.pack()
A3.pack()
A4.pack()
A5.pack()
A6.pack()
A7.pack()
A8.pack()

B1.pack()
B2.pack()
B3.pack()
B4.pack()
B5.pack()
B6.pack()
B7.pack()
B8.pack()

C1.pack()
C2.pack()
C3.pack()
C4.pack()
C5.pack()
C6.pack()
C7.pack()
C8.pack()

D1.pack()
D2.pack()
D3.pack()
D4.pack()
D5.pack()
D6.pack()
D7.pack()
D8.pack()

E1.pack()
E2.pack()
E3.pack()
E4.pack()
E5.pack()
E6.pack()
E7.pack()
E8.pack()

F1.pack()
F2.pack()
F3.pack()
F4.pack()
F5.pack()
F6.pack()
F7.pack()
F8.pack()

G1.pack()
G2.pack()
G3.pack()
G4.pack()
G5.pack()
G6.pack()
G7.pack()
G8.pack()

H1.pack()
H2.pack()
H3.pack()
H4.pack()
H5.pack()
H6.pack()
H7.pack()
H8.pack()
I'm planning on having a separate tkinter window with a list of buttons. When the player clicks a button, which represents a spot on the board, they get a list of available pieces they can move to that spot. Only problem is, I'm not sure how to snap the pieces to one of the squares. How do I snap a piece to a specific spot on the tkinkter window? Also if you know how I could make my code shorter, let me know. Thanks
Reply
#2
(Jan-12-2019, 04:34 PM)Trinx Wrote: Also if you know how I could make my code shorter, let me know. Thanks
Your code can be massively condensed from simple for loops. For example, instead of packing each individual button separately, put them in a list and pack them all. You can do the same with creating the Button as the only thing that is changing is the text.
Recommended Tutorials:
Reply
#3
(Jan-12-2019, 06:29 PM)metulburr Wrote:
(Jan-12-2019, 04:34 PM)Trinx Wrote: Also if you know how I could make my code shorter, let me know. Thanks
Your code can be massively condensed from simple for loops. For example, instead of packing each individual button separately, put them in a list and pack them all. You can do the same with creating the Button as the only thing that is changing is the text.

How can I do that? Is there a way to do a loop and add/subtract a certain number each time the loop repeats it's self? Would I use the "for in range(#)" command somehow? I agree, it's kinda crazy how much repeating there is.
Reply
#4
surprisingly we dont have tutorial on just simple for loops. But yes
for num in range(1,9):
num each loop will be from 1-8
this can reduce you canvas.create_text, canvas.create_line, canvas.create_rectangleyour Button creation, and your packing.
more info on loops here
https://www.tutorialspoint.com/python/py...r_loop.htm
Recommended Tutorials:
Reply
#5
(Jan-12-2019, 11:15 PM)metulburr Wrote: surprisingly we dont have tutorial on just simple for loops. But yes
for num in range(1,9):
num each loop will be from 1-8
this can reduce you canvas.create_text, canvas.create_line, canvas.create_rectangleyour Button creation, and your packing.
more info on loops here
https://www.tutorialspoint.com/python/py...r_loop.htm

How do I change the value in the code though? Like for example, this code:

lineh1 = canvas.create_line(75, 0, 75, 600)
lineh2 = canvas.create_line(150, 0, 150, 600)
On the second line of code, the two 75s get 75 added to them with each new line. How would I do the loop and add a number to a value each time it repeats itself? Thanks for helping.
Reply
#6
Quote:How do I change the value in the code though? Like for example, this code:

lineh1 = canvas.create_line(75, 0, 75, 600)
lineh2 = canvas.create_line(150, 0, 150, 600)
On the second line of code, the two 75s get 75 added to them with each new line. How would I do the loop and add a number to a value each time it repeats itself?

you would do something like this
for num in range(1,9):
    multiplier = 75
    canvases.append(canvas.create_line(multiplier*num, 0, multiplier*num, 600))
Recommended Tutorials:
Reply
#7
Quote:you would do something like this
for num in range(1,9):
    multiplier = 75
    canvases.append(canvas.create_line(multiplier*num, 0, multiplier*num, 600))

Cool, I'll try that, thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Moving chess piece using mouse reepio13 7 6,904 Feb-24-2018, 11:26 PM
Last Post: reepio13

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020