[Python] Try using Tkinter's canvas

Introduction

The language Python has a toolkit called "Tkinter" that creates interfaces by default. This Tkinter is convenient because you can create a simple GUI without adding plugins unnecessarily, but there is no explanation site. So, I would like to summarize the parts that I have researched as needed.

About Tkinter

I wrote the basics in Try using Tkinter in Python, so if you are interested ** please. I will write it so that I can create it without reading it.

Try using Tkinter

How to use Tkinter easily. For more information, please check it yourself or read "Try using Tkinter in Python".

Python


#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import Tkinter

#
#GUI settings
#
root = Tkinter.Tk()
root.title(u"Try using Tkinter's Canvas")
root.geometry("800x450")   #Window size (specified by "width x height")



#
#GUI end
#
root.mainloop()

Try using Tkinter's Canvas

The main story starts here. I'm going to write it so that it works with basic copy, but please understand that I will not touch on the basics of Tkinter so much. For more information, please check it yourself or read "Try using Tkinter in Python".

Canvas definition

Canvas is written in GUI processing like other objects in Tkinter (buttons, entries, etc.). Please see the actual sample.

Python


#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import Tkinter

#
#GUI settings
#
root = Tkinter.Tk()
root.title(u"Try using Tkinter's Canvas")
root.geometry("800x450")

#Canvas area
canvas = Tkinter.Canvas(root, width = 800, height = 450)

#Canvas bind
canvas.place(x=0, y=0)


#
#GUI end
#
root.mainloop()

This created an 800px * 450px Canvas and placed it at (x, y) = (0,0).

As the content, canvas = Tkinter.Canvas(root, width = 800, height = 450) Canvas is created by this process canvas.place(x=0, y=0) Canvas is placed on the screen by this process.

I think it's hard to understand with just this, so canvas.create_rectangle(0, 0, 800, 450, fill = 'green', stipple = 'gray25') I will put in.

Python


#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import Tkinter

#
#GUI settings
#
root = Tkinter.Tk()
root.title(u"Try using Tkinter's Canvas")
root.geometry("800x450")

#Canvas area
canvas = Tkinter.Canvas(root, width = 800, height = 450)#Creating Canvas
canvas.create_rectangle(0, 0, 800, 450, fill = 'green')#fill

#Canvas bind
canvas.place(x=0, y=0)#Canvas placement


#
#GUI end
#
root.mainloop()

If you run the above sample, you will see a window with a size of 800px * 450px and a green rectangle that fills the screen. スクリーンショット 2015-05-12 11.21.01.png

I added canvas.create_rectangle(0, 0, 800, 450, fill = 'green') In one line, this fills the specified part of the Canvas with a specific color.

In this way, the created Canvas also writes the process from the creation point to the bind point.

Things that can be drawn with Canvas

Details are described in Introduction to Easy Python / Tkinter, but here is also a quote.

function Description
create_line() Straight line (line)
create_oval() ellipse
create_arc() Arc (part of the circumference of an ellipse)
create_rectangle() Rectangle
create_polygon() Polygon
create_image() image
create_bitmap() bitmap
create_text() String

In addition, we will introduce some commonly used options.

processing Description
fill =color Color to fill the inside
stipple =bitmap Bitmap that becomes a pattern when filling the inside
outline =color Frame color
width =width Frame width (default is 1).0)

Tag objects (try to erase after drawing)

Each object has a "tag". If you specify this tag, you can erase it after drawing.

Python


#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import Tkinter

#
#GUI settings
#
root = Tkinter.Tk()
root.title(u"Try using Tkinter's Canvas")
root.geometry("800x450")


#When the "Draw" button is pressed
def draw(event):
	canvas.create_oval(10, 10, 140, 140, tag="oval")
	


#When the "Erase" button is pressed
def erase(event):
	canvas.delete("oval")
	


#Canvas area
canvas = Tkinter.Canvas(root, width = 800, height = 300)

#Draw a circle
canvas.create_oval(10, 10, 140, 140, tag="oval")

#Canvas bind
canvas.place(x=0, y=0)



#"Draw" button
button_draw = Tkinter.Button(root, text=u'Draw',width=15)
button_draw.bind("<Button-1>",draw)
button_draw.place(x=100,y=350)


#"Erase" button
button_draw = Tkinter.Button(root, text=u'Erase',width=15)
button_draw.bind("<Button-1>",erase)
button_draw.place(x=350,y=350)


#
#GUI end
#
root.mainloop()

When this is executed, the following window will be created. スクリーンショット 2015-05-13 21.03.32.png

If you press the "Erase" button, the circle will disappear, and if you press the "Draw" button, the circle will be drawn again. At this time canvas.create_oval(10, 10, 140, 140, tag="oval") So, if you specify the "tag" attribute for the object you draw like tag =" oval ", canvas.delete("oval") You can delete an object with any tag attribute like this.

That's all for now.

Recommended Posts

[Python] Try using Tkinter's canvas
Try using Tweepy [Python2.7]
Try using Kubernetes Client -Python-
Try python
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Try using Python argparse's action API
Try using the Python Cmd module
Try using Leap Motion in Python
Try using Amazon DynamoDB from Python
Try using Tkinter
Try using the Wunderlist API in Python
Try using docker-py
Try mathematical formulas using Σ with python
Try using the Kraken API in Python
Try using cookiecutter
Try using PDFMiner
Try using Dialogflow (formerly API.AI) Python SDK #dialogflow
Start using Python
Try using geopandas
Try using Python with Google Cloud Functions
Try using Selenium
Try using scipy
Python> try: / except:
Try using Junos On-box Python # 2 Commit Script
Try to operate Excel using Python (Xlwings)
Try using pandas.DataFrame
Try using Junos On-box Python # 1 Op Script
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try using PyODE
Scraping using Python
Try using the BitFlyer Ligntning API in Python
Python: Try using the UI on Pythonista 3 on iPad
Try using the Python web framework Tornado Part 1
Try using the collections module (ChainMap) of python3
Try using tensorflow ① Build python environment and introduce tensorflow
Try using the Python web framework Tornado Part 2
Try using ChatWork API and Qiita API in Python
Try using the DropBox Core API in Python
Try scraping with Python.
Operate Redmine using Python Redmine
Fibonacci sequence using Python
python try ~ except ~ else
Data analysis using Python 0
Try using virtualenv (virtualenvwrapper)
Data cleaning using Python
[Azure] Try using Azure Functions
Try Debian + Python 3.4 + django1.7 ...
Using Python #external packages
Try using virtualenv now
WiringPi-SPI communication using Python
Try gRPC in Python
Try using W & B
Age calculation using python
Try using Django templates.html
[Kaggle] Try using LGBM
Try using Python's feedparser.
Try using Python's Tkinter
Search Twitter using Python