Python Forum
[Tkinter] Adding graphics to canvas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Adding graphics to canvas
#1
I have 2 parts of code like this

import tkinter
import tkinter.messagebox
import turtle
import numpy as np
from turtle import*
turtle.tracer(0,0)
wn = turtle.Screen()
wn.bgcolor("light blue")
wn.title("Turtle")
skk = turtle.Turtle()
skk.hideturtle()
skk.speed(800)
skk.pensize(3)

def Board2():
   
    for i in range(13):
            skk.penup()
            skk.goto(-650,-300+i*50)
            skk.pendown()
            skk.forward(1200)
            skk.penup()
    for i in range(25):
            skk.penup()
            skk.goto(-650+i*50,-300)
            skk.pendown()
            skk.left(90)
            skk.forward(600)
            skk.right(90)
            skk.penup()
d=0
val=np.zeros((25,13))
def click(x,y):
 print("trc lam tron")
 print(x,y)
 if((x<-675 or x>575) or (y<-325 or y>325)):
      tkinter.messagebox.showinfo("You fool!!","Wrong position motherfucker!!!")
 else:
    if (x%100<=75 and x%100>=25):
            x=x-x%100+50
    else:
        if(x%100<=25 and x%100>=0):
            x=x-x%100
        else: x=x+100-x%100

    if (y%100<=75 and y%100>=25):
            y=y-y%100+50
    else:
        if(y%100<=25 and y%100>=0):
            y=y-y%100
        else: y=y+100-y%100
    print("sau lam tron")
    print(x,y)
    global val
    a=(x+650)//50
    b=(y+300)//50
    a=int(a)
    b=int(b)
    if(val[a][b]==1) or (val[a][b]==2.0):
       tkinter.messagebox.showinfo("You fool!!","This shit has already taken motherfucker!!!")
    else:
            global d
            if (d%2==0):
             X(x,y)
             val[a][b]=1
            else:
             O(x,y)
             val[a][b]=2
            d=d+1
    

def X(x,y):
    skk.pensize(8)
    skk.pencolor("blue")
    skk.penup()
    skk.goto(x-12.5,y-12.5)
    skk.pendown()
    skk.goto(x+12.5,y+12.5)
    skk.penup()
    skk.goto(x-12.5,y+12.5)
    skk.pendown()
    skk.goto(x+12.5,y-12.5)
    skk.penup()
def O(x,y):
    skk.pensize(8)
    skk.pencolor("red")
    skk.penup()
    skk.goto(x,y-12.5)
    skk.pendown()
    skk.circle(12.5,360)
    skk.penup()


Board2()
onscreenclick(click)

turtle.update()
turtle.done()
And this
import tkinter
import turtle
import tkinter.messagebox

window = tkinter.Tk()

canvas = tkinter.Canvas(master = window, width = 800, height = 800)
canvas.grid(padx=2, pady=2, row=0, column=0, rowspan=10, columnspan=10) # , sticky='nsew')

draw = turtle.RawTurtle(canvas)

def Board(a, x, y, size):
    #draw.pu()
    draw.penup()
    draw.goto(x,y)
    #draw.pd()
    draw.pendown()
    for i in range (0, 4):
        draw.forward(size)
        draw.right(90)

def Board2():
    #the whole codes above goes here

def Button_click ():
    tkinter.messagebox.showinfo("Game", "Tic Tac Toe")


Play_Button = tkinter.Button(master = window, text ="Play!", command = Button_click)
Play_Button.config(bg="cyan",fg="black")
Play_Button.grid(padx=2, pady=2, row=0, column=11, sticky='nsew')

Board_Button = tkinter.Button(master = window, text ="Draw_Board", command = Board)
Board_Button.config(bg="cyan",fg="black")
Board_Button.grid(padx=2, pady=2, row=1, column=11, sticky='nsew')
window.mainloop()
I'm intending to write a function that will display the first part of codes into the function name Board in the above codes.
However, when I wrote the codes in Board function and compile it, 2 windows appeared (one is from the turtle and one from the canvas). What did I go wrong?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Graphics CANVAS resolution brievenbusrjvos 1 2,975 Feb-17-2020, 03:23 AM
Last Post: Larz60+
  [Tkinter] Resizing image inside Canvas (with Canvas' resize) Gupi 2 25,021 Jun-04-2019, 05:05 AM
Last Post: Gupi
  [Tkinter] Need some basic help with GUI graphics Jerz 2 2,928 Nov-11-2017, 12:10 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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