w3resource

Python tkinter widgets Exercise: Add a canvas in your application using tkinter module

Python tkinter widgets: Exercise-2 with Solution

Write a Python GUI program to add a canvas in your application using tkinter module.

Sample Solution:

Python Code:

import tkinter as tk 
parent = tk.Tk() 

canvas_width = 100
canvas_height = 80
w = tk.Canvas(parent, 
           width=canvas_width,
           height=canvas_height)
w.pack()

y = int(canvas_height / 2)
w.create_line(0, y, canvas_width, y, fill="#476042")
parent.mainloop()

Sample Output:

Flowchart: Add a canvas in your application using tkinter module

Flowchart:

Flowchart: Add a canvas in your application using tkinter module

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python GUI program to add a button in your application using tkinter module.
Next: Write a Python GUI program to create two buttons exit and hello using tkinter module.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz


Python: Tips of the Day

Performs left-to-right function composition.

Example:

from functools import reduce

def tips_compose_right(*fns):
  return reduce(lambda f, g: lambda *args: g(f(*args)), fns)
add = lambda x, y: x + y
square = lambda x: x * x
add_and_square = tips_compose_right(add,square)

print(add_and_square(5, 2))

Output:

49