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:
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
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework