How to redraw on a line in Tkinter canvas - python

I have some lines in tkinter canvas, and also have their code. I want to make them red but not instantaneously, and I want to another line(red line) on them but it should take different time. For example for one specific line it should take 3 seconds that line get red for another one it should take 7 seconds to make that red. It is like drawing another red line on the previous one.
def activator(self, hexagon, duration_time):
if not hexagon.is_end:
self.canvas.itemconfigure(hexagon.drawn, fill="tomato")
self.canvas.itemconfigure(hexagon.hex_aspects.outputs.drawn, fill="tomato")
For example I want my hexagon which created by create_polygon method of tkinter get red but not immediately. It should do regarding to duration_time which is the a second variable. I mean it should be done within duration_time second (let say 3 seconds).
Is there any way for doing this? I have lots of object in my canvas which should get red during an specific time. line, circle, polygon..

To do that, you could just draw over it and have a function for drawing over each for a different amount of time:
import time
def redraw(delay, color, canvas):
time.sleep(delay)
#some parentheses are for the arguments
(canvas).create_(shape)(blah, outline=(color), fill=(color))
for every shape, but for lines, you do not do the fill argument.

To draw things in the canvas, use the create methods to add new items.
from Tkinter import *
master = Tk()
w = Canvas(master, width=200, height=100)
w.pack()
w.create_line(0, 0, 200, 100)
w.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
w.create_rectangle(50, 25, 150, 75, fill="blue")

Related

Tkinter Polygon's Width not being set to zero even though specified

I need to draw a few Polygons using tkinter and so I tried the following code:
from tkinter import *
master = Tk()
w = Canvas(master, width=200, height=200)
w.pack()
points = [0, 0, 200, 100, 0, 200]
w.create_polygon(points, outline="green",fill='yellow', width=0)
mainloop()
and I get the following output
But the problem is... the width isn't being set to 0. Is this is an internal issue with Tkinter or is there a way I can fix this?
You are specifying a color for an outline, so the canvas must draw an outline that is at least one pixel wide in order to show the outline. If you don't want an outline, set outline to None or the empty string.

Why does my create_rectangle disappears after specifying these points using tkinter?

I wanted to create a transparent box with a red outline between these two specified points at (614, 162) and (759, 306). However, it returns me an invisible or no box at all. But for (100, 50) and (160, 100), the box is visible.
import tkinter as tkr
app = tkr.Tk()
app.title("AI Cashier")
app.geometry("1366x768")
app.wm_attributes("-transparentcolor", "white")
app.config(bg = "White")
can = tkr.Canvas(app,bg = "White",highlightthickness = 0)
can.create_rectangle(100,50,160,100,outline = "red", width = 2)
can.pack()
app.mainloop()
Could anyone explain to me because I'm still new to tkinter. Or is it a bug?
Why does my create_rectangle disappears after specifying these points using tkinter?
It is because the canvas is only a couple hundred pixels wide and tall, so you are drawing outside the visible area of the canvas.
A simple fix for the code in the question is to make the canvas bigger. You can either give it an explicit width and height (eg: can = tkr.Canvas(..., width=800, height=400)), or force the canvas to fill the window (eg: can.pack(fill='both', expand=True')). In either of those cases, the image will be visible.

How to display a grid on a canvas tkinter object?

So I'm creating the game of life in python, and would like to create a grid on top of a canvas using the tkinter class of python. Thanks in advance.
To make the grid I'm using the create_line() function of the canvas class and looping through using the range() function so that my lines are drawn to the width and the height of the canvas, using two separate for loops one for width and one for the height. The loops I have gotten are taken from the following piece of code on stackoverflow: How to create a grid on tkinter in python?
I thought I understood what is happening but what I thought should happen has not
from tkinter import *
from random import *
window = Tk()
window.title('Game Of Life')
canvas = Canvas(window, background='white', width=800, height=600)
def create_grid(canvas):
width = canvas.winfo_width() # gets width of the canvas
height = canvas.winfo_height() # gets height of the canvas
for line in range(0, width, 1): # range(start, stop, step)
canvas.create_line([(line, 0), (line, height)], fill='black', tags='grid_line_w')
for line in range(0, height, 1):
canvas.create_line([(0, line), (width, line)], fill='black', tags='grid_line_h')
create_grid(canvas)
canvas.grid(row=0, column=0)
window.mainloop()
My expected results are to have a white canvas with vertical and horizontal lines stretching the width and height of the canvas. But my actual results are just a white canvas with no lines on top.
I don't trust winfo_width and winfo_height to give accurate values in this context. According to https://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.winfo_height-method, the functions simply return 1 if you haven't packed/gridded/placed the widget yet, and even if you have, it might still return 1 if the window's event loop hasn't updated lately. You already know that you want the width to be 800 and the height to be 600, so you may as well define them as such directly.
Another problem is that your range calls have a step argument of 1. This means that the lines will be one pixel apart, which will effectively paint the entire canvas black. I recommend a larger step.
from tkinter import *
from random import *
window = Tk()
window.title('Game Of Life')
def create_grid(window):
width = 800
height = 600
canvas = Canvas(window, background='white', width=width, height=height)
for line in range(0, width, 10): # range(start, stop, step)
canvas.create_line([(line, 0), (line, height)], fill='black', tags='grid_line_w')
for line in range(0, height, 10):
canvas.create_line([(0, line), (width, line)], fill='black', tags='grid_line_h')
canvas.grid(row=0, column=0)
create_grid(window)
window.mainloop()
Result:

Can't fill colour in tkinter arc in python.

The Code:-
from tkinter import *
import sys
import time
import random
root = Tk()
canvas = Canvas(root,height=700,width=700,bg='pink')
canvas.pack()
canvas.create_rectangle(0,0,10,700,fill='blue')
canvas.create_rectangle(690,0,700,700,fill='blue')
canvas.create_rectangle(0,0,700,10,fill='blue')
canvas.create_rectangle(0,690,700,700,fill='blue')
canvas.create_arc(110,9,130,29,extent=359,fill='black',style=ARC)
canvas.create_rectangle(290,500,410,510,fill='red')
root.mainloop()
Everything appears fine with color, only the arc doesn't get filled up.
How can I color the arc?
According to the documentation, you cannot fill an arc except if its style is either PIESLICE or CHORD.
This actually makes sense: how would you fill a geometric object which is not closed?
You can simply remove the style attribute and it should work.
canvas.create_arc(110, 9, 130, 29, extent=359, fill='black')
I find it easier to use the outline = colour method while leaving the entry of style = 'arc' alone this will change the colour to what you want because of a tkinter querk that for small line widths you only ever see the outline. A more thorough way would be outline = colour, fill = colour this will be a complete uniform color no matter the thickness

Python GUI and TKinter Grid

I'm new to Python, and I'm trying to write a program that displays an 8x8 grid. The vertical lines are supposed to be red and the horizontal lines are supposed to be blue. But I can't seem to figure it out. I know it must be in a loop, but I'm not sure even where to start. Please help!
Here's my code so far:
from tkinter import *
class Canvas:
def __init__(self):
self.window = Tk()
self.window.title("Grid")
self.canvas = Canvas(window, width = 200, height = 200,
bg = "white")
self.canvas.pack()
def drawGrid(self):
self.canvas.create_line()
Thanks!
Take a look at http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_line-method for details about the create_line() method.
You need two arguments: the coordinates of the line and a fill color. The coordinates are a list of [x0, y0, x1, y1], and they correspond to pixel values with an origin at the top-left corner of the parent widget, so to draw a horizontal green line on your 200x200 Canvas, you'd write:
self.canvas.create_line(0,0,200,200, fill='green')
To create a grid of lines, a for or while loop would work, which modified the list of coordinates at every iteration and passed it to a new create_line() function at the end of each loop.
That should get you started.

Categories