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.
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.
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()
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 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.
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.
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) |
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.
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