[2] tkinter GUI supplement

catalogue

Write in front    

1. Canvas

The method is as follows: a. create_arc(options)  

b. create_bitmap(x, y, b)

c. create_image(x, y, image)

d. "Point"

2. Events

a. Mouse event

b. Keyboard events

c. Use bind()

3. Menu control

a. toplevel menu

b. pulldown menu

c. Pop up shortcut menu

Highlights of previous periods

​​​​​​​

Write in front    

I believe everyone should have read the first article in this ID series, about tkinter.

If you don't see it, hurry to see it!!!

Then I looked through my information. Well, it's delicious. Most of them didn't tell me. So I told him today.

Attention, today is serious! Serious today!
Let's go. Stop it.

1. Canvas

Canvas is similar to the canvas in html5. It is mainly used to draw pictures, such as arcs, pictures, lines, etc. This space is mainly the use of methods.

The method is as follows:
a. create_arc(options) 

create_arc is used to create arcs. The parameters include coord, start, extend and fill.

coord defines the coordinates of the upper left and lower right corners of the arc (the unit should be pixel), start defines the starting angle of the arc, ext represents the end angle, and fill is the color of the filled block.

example:

from tkinter import *
f = Tk()
coord = 13, 60, 260, 240
canvas = Canvas(f)
#Using create_arc method
canvas.create_arc(coord, start=10, extent=200, fill='blue')
canvas.pack()
f.mainloop()

effect:

design sketch

b. create_bitmap(x, y, b)

This method is used to create a bitmap.

Several parameters are given here: x and y are used to define the upper left corner of the bitmap, and b is the source of the bitmap, such as hourglass, error, info, question, etc.

example:

from tkinter import *
f = Tk()
canvas = Canvas(f)
canvas.create_bitmap(40, 40, bitmap='info')
canvas.pack()
f.mainloop()

effect:

design sketch

c. create_image(x, y, image)

This control is used to create an image. x and y define the coordinates of the upper left corner of the picture, and image defines the source of the picture. It must be an instance variable of BitmapImage class or PhotoImage class of tkinter module.

Example: (this case loads a picture named demo.jpg. If you need to import your own picture, please change it.)

from tkinter import *
f = Tk()
img = PhotoImage(file='demo.jpg')
canvas = Canvas(f)
canvas.create_image(40, 80, image=img)
canvas.pack()
f.mainloop()

The three methods have been mentioned above. In fact, the general methods are the same. Then list some common methods, and I won't introduce them in detail:

methodbrief introduction
create_line(x0, y0, x1, y1, ..., xn, yn, options)Create Line
create_oval(x0, y0, x1, y1, options)Create a circle
create_polygon(x0, y0, x1, y1, ..., xn, yn, options)Create a polygon
create_rectangle(x0, y0, x1, y1, options)Create a rectangle

d. "Point"

We regret that there is no point drawing in Python.

However, there is still a painting method. In the article in the link below, we talk about how to draw "points" in detail. You can see it.
Original link: Poke mehttps://blog.csdn.net/qq_41556318/article/details/85272026

2. Events

We need a way to detect the "things to do" of the mouse or keyboard, which is the event.

We focus on "mouse event" and "keyboard event".

a. Mouse event

In mouse events, 1 represents the left button, 2 represents the middle button, and 3 represents the right button. Mouse events include:

Mouse event
eventintroduce
<Enter>Mouse pointer entry control
<Leave>The mouse pointer leaves the control
<Button-1 / 2 / 3>, <ButtonPress-1 / 2 / 3>, <1 / 2 / 3>Mouse (left / middle / right) press
<B1-Motion>Drag the form with the left button
<ButtonRelease-1>Release the left key
<Double-Button-1>Double click the left button

b. Keyboard events

Keyboard events are:

eventsummary
<Key>Occurs when ASCII codes 48-90 are pressed, i.e. numbers, letters and symbols such as +, -.
<Control / Shift / Alt -Up / Down / Left / Right>Occurs when the front rear key combination is pressed.
For other keys, use the name directlySuch as < return > < escape >.

c. Use bind()

The bind() method to use the events described above.

Definition: bind(event, cb) sets the event handler cb(callback).

We can see the menu case in 3.c of this article. Our right-click "detection" uses the event and bind() methods.

3. Menu control

Menu control can create three types of menus, namely shortcut menu, home directory and drop-down menu.

This control can call some methods to use:

Method of Menu control
methodexplain
add_command(options)Add a new menu item.
add_radiobutton(ooptions)Create a selection button menu item
add_checkbutton(options)Create a check box menu item
add_cascade(options)Link a specified menu with its parent menu to create a new cascading menu
add_separator()Split line
add(type, options)New special type menu item
delete(startindex [, endindex])Delete the menu item from startindex to endindex
entryconfig(index, options)Modify the menu item of index

Without much nonsense, let's take a look at the examples:

a. toplevel menu

from tkinter import *
import tkinter.messagebox
f = Tk()
def doSth():
    tkinter.messagebox.showinfo('hello', 'toplevel_menu')

mainmenu= Menu(f)

mainmenu.add_command(label='XIAOMANONGdeMENU', command=doSth)
mainmenu.add_command(label='nitingdonglema', command=doSth)

f.config(menu=mainmenu)


win.mainloop()

Save and run the program. It can be seen that several menus appear on the interface. Click it to open a dialog box (function).

b. pulldown menu

from tkinter import *
import tkinter.messagebox

f = Tk()


# Define several functions for testing
def doA():
    tkinter.messagebox.askokcancel('A', 'you touch A!')


def doB():
    tkinter.messagebox.askokcancel('B', 'you touch B!')


def doC():
    tkinter.messagebox.askokcancel('C', 'you touch C!')


def doD():
    tkinter.messagebox.askokcancel('D', 'you touch D!')


mainmenu = Menu(f)

demomenu = Menu(mainmenu, tearoff=0)
demomenu.add_command(label='A', command=doA)
demomenu.add_command(label='B', command=doB)
demomenu.add_command(label='C', command=doC)
demomenu.add_command(label='D', command=doD)

mainmenu.add_cascade(label='DEMO', menu=demomenu)


f.mainloop()

c. Pop up shortcut menu

from tkinter import *
import tkinter.messagebox

f = Tk()

def gogo():
    tkinter.messagebox.askokcancel('hello', 'gogopython!')

menu= Menu(f, tearoff=0)

menu.add_command(label='1', command=gogo)
menu.add_command(label='2', command=gogo)

#Show this menu

def showmenu(event):
    menu.post(event.x_root, event.y_root)

win.bind("<Button-3>", showmenu)

win.mainloop

Well, that's all for today's blog. I'll see you next time.  

---------------------------------------------------------------------------

After two blog s, have you paid your tuition?

Highlights of previous periods

[1] tkinter doesn't work. What else do you want?

Tags: Python GUI

Posted on Sat, 25 Sep 2021 00:16:40 -0400 by mcollyns