Use the tkinter module in Python to plot and pythontkinter to plot

Source: Internet
Author: User
Tags draw box

Use the tkinter module in Python to plot and pythontkinter to plot

Tkinter can be used to create complete applications, such as simple word processing software and simple drawing software.

1. Create a button that can be clicked and use tkinter to create a simple program with buttons. The Code is as follows:
>>> from tkinter import*>>> tk = Tk()>>> btn = Button(tk,text = "click me")>>> btn.pack()

 

In the first line, we introducedTkinterModule Content. Use the from Module name import * to use the module content without the module name.

The buttons we created are as follows:

Note: This "press me" button does not do anything unless we modify some code (do not forget to close the previously created window first ).

First, we create a function to print some text:
>>> def hello():    print('hello world')

 

Modify our example to use this new function:
>>> from tkinter import*>>> tk = Tk()>>> btn = Button(tk,text = "click me",command = hello)>>> btn.pack()

 

Now, when we click the button, we will see "hello" in the Shell program ". Each time you click a button, you will see it. The effect is as follows:

(This is the first time we use the "name function". before continuing to draw a picture, let's take a look at this function)

 

Ii. Use the name Function

The name function is the same as a common parameter, but it does not determine which value a parameter obtains in the Order provided by the function. We define the name of the value explicitly, therefore, it can be written in any order.

The named function allows us to only provide values for the parameters we want to assign values. The following is an example.

Suppose we have a function called person, which has two parameters: width and height ).

>>> def person(width,height):    print('I am %s feet wide,%s feet high' % (width,height))

 

Generally, we call it like this:

>>> person(4,3)I am 4 feet wide,3 feet high

 

Using the named parameter, we can call the function and specify the parameter to which each value is assigned:

>>> person(height=3,width=4)I am 4 feet wide,3 feet high

 

As you can see, although the order of pre-and post-assigned values is different, the results are the same.

 

3. Create a canvas for drawing

To draw a picture, we need a different element:Canvas)Object, that is, the Canvas Class Object (provided by the tkinter module ).

When we create a canvas, we pass the width and height (in pixels) of the canvas to Python ). In other aspects, the Code is the same as that of the button:

>>> from tkinter import*>>> tk = Tk()>>> canvas = Canvas(tk,width=500,height=500)>>> canvas.pack()

 

Note:Pack FunctionIs to make the canvas display in the correct position. If this function is not called, nothing will be displayed normally.

 

4. Draw a line

Pixel coordinates are required to draw lines on the canvas.

Generally, the upper left corner of the canvas is the start coordinate (500,500), and the lower right corner of the canvas is the end coordinate ). (Based on the size of the canvas created above)

We useCreate_line FunctionTo specify these coordinates, as shown below:

>>> canvas.create_line(0,0,500,500)1

 

Function create_lineReturns 1, which is a flag. If we want to use the turtle module to do the same thing, we need the following code:

>>> import turtle>>> turtle.setup(width=500,height=500)>>> t=turtle.Pen()>>> t.up()>>> t.goto(-250,250)>>> t.down()>>> t.goto(500,-500)
Turtle module Line Drawing

The effect is as follows:

 

5. Draw box 1 and draw a square

With the turtle module, we can draw a box by moving forward, turning, moving forward, turning, and so on. Finally, we can draw a rectangle by changing the distance moving forward.

However, it is much easier to use the tkinter module to draw a rectangle. We only need to know the coordinates of each angle, for example:

>>> From tkinter import * >>> tk = Tk () >>>> canvas = Canvas (tk, width = 400, height = 400) >>> canvas. pack () >>> canvas. create_rectangle (10, 10, 50, 50) # (10, 10) is the coordinate of the upper right corner of the square, (50, 50) is the coordinate of the lower right corner of the square

 

 

In this Code, we use tkinter to create a canvas with a width of 400 pixels and a height of 400 pixels, and then draw a square in the upper left corner of the window. The effect is as follows:

 

 

2. Draw a rectangle

Next I will try to draw two rectangles (the first four lines of code are the same, just the last lineCanvas. create_rectangle FunctionYou need to change the coordinate parameters ):

>>> canvas.create_rectangle(10,10,300,50)1>>> canvas.create_rectangle(10,10,50,300)2

 

 

3. Draw many rectangles.

Introduce the random number module of random, and write a function to use the random number as the coordinates of the upper left corner and lower right corner of the rectangle.

The random module provides a randrange function. This function is used to return a random integer between 0 and this number when a number is input to this function. For example, if randrange (10) is called, a value ranging from 0 to 10 is returned ~ A number between 9, randrange (100) will return a 0 ~ A number between 99.

From tkinter import * import randomtk = Tk () canvas = Canvas (tk, width = 400, height = 400) canvas. pack () def random_rectangle (width, height): x1 = random. randrange (width) # create the variable x1 and set it to a random number y1 = random from 0 to width. randrange (height) x2 = x1 + random. randrange (width) # create variable x2, which is calculated from the previous x1 plus a random number y2 = y1 + random. randrange (height) canvas. create_rectangle (x1, y1, x2, y2) # Use the x1, y1, x2, y2 variables to call canvas. create_rectangle draw a rectangle for x in range (0,100) on the canvas: # Use the for loop to draw 100 random rectangles random_rectangle (400,400)
Many rectangles

(Does it feel like a modern work of art)

 

4. Set the color.

ChangeRandom_rectangle FunctionTo input an additional parameter.(Fill_color)To specify the color of the rectangle.

From tkinter import * import randomtk = Tk () canvas = Canvas (tk, width = 400, height = 400) canvas. pack () def random_rectangle (width, height, fill_color): x1 = random. randrange (width) # create the variable x1 and set it to a random number y1 = random from 0 to width. randrange (height) x2 = x1 + random. randrange (width) # create variable x2, which is calculated from the previous x1 plus a random number y2 = y1 + random. randrange (height) canvas. create_rectangle (x1, y1, x2, y2, fill = fill_color) # fill_color is used as a parameter to specify the color random_rectangle (400,400, 'green') random_rectangle (400,400, 'red') random_rectangle (400,400, 'blue') random_rectangle (400,400, 'Orange ') random_rectangle (400,400, 'yellow') random_rectangle (400,400, 'pink ') # Pink random_rectangle (400,400, 'purple ') # purple random_rectangle (400,400, 'viot') # violet random_rectangle (400,400, 'magenta ') # Product Red random_rectangle (400,400, 'cyany') # Blue-green
Colored rectangle

 

Think: What if we want to customize a color that is not exactly the same as the named color?

 

6. Draw an arc

An arc is a circular section or a curve. To use tkinter to draw an arc, we need to useCreate_arc FunctionPlot in a rectangle:

>>> from tkinter import*>>> tk = Tk()>>> canvas = Canvas(tk,width=400,height=400)>>> canvas.pack()>>> canvas.create_arc(10,10,200,100,extent=180,style=ARC)

 

The coordinates in the upper left corner of the Arc are set to (200,100), and those in the lower right corner are ).ExtentIs used to specify the arc angle.

Next we will draw several different arcs from top to bottom on the page, so that we can see the effects of different angles on the create_arc function:

>>> canvas.create_arc(10,10,200,80,extent=45,style=ARC)>>> canvas.create_arc(10,80,200,160,extent=90,style=ARC)>>> canvas.create_arc(10,160,200,240,extent=135,style=ARC)>>> canvas.create_arc(10,240,200,320,extent=180,style=ARC)>>> canvas.create_arc(10,320,200,400,extent=359,style=ARC)

Note: When we draw the last circle, we use 359 degrees instead of 360 degrees, because tkinter regards 360 degrees as 0 degrees. If we use 360 degrees, we cannot draw anything.

 

7. Draw a polygon

When we use tkinter to draw a polygon, you need to provide coordinates for each vertex of the polygon. The method of triangle is as follows:

>>> from tkinter import*>>> tk = Tk()>>> canvas = Canvas(tk,width=400,height=400)>>> canvas.pack()>>> canvas.create_polygon(10,10,100,10,100,110,fill="",outline="black")

 

Start from x, y coordinates (100,110), draw to (), and then end ).

Next we will draw an irregular polygon:

>>>canvas.create_polygon(200,10,240,30,120,100,140,120,fill="",outline="black")

 

Start from coordinates (120,100), draw to (100,140), then draw to (), and end ). Tkinter automatically draws the coordinates from the first line.

 

Summary

This time, we learned how to use the tkinter module to create buttons and named functions, draw simple Ry on the canvas, and learn how to color.

In the future, we will learn how to use the tkinter module to display text and images and try to create basic animations. Pai_^

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.