Python tkinter -- Chapter 18 Bitmap of Canvas Control (bitmap)

18.2.15 create_bitmap(position, ** options)
creates a bitmap at the specified position on the canvas.
(1) position: the coordinates (x, y) of the bitmap.
(2) **options: The options are:

options meaning
activebackground Specifies the background color when the bitmap object state is ACTIVE
activebitmap Specifies the bitmap to fill when the bitmap object state is ACTIVE
activeforeground Specifies the foreground color when the bitmap object state is ACTIVE
anchor 1. Specify the relative position of the bitmap in the position parameter
2. N, NE, E, SE, S, SW, W, NW, or CENTER for positioning (EWSN stands for east, west, south, north, up north, down south, left west, right east) 3
. The default value is CENTER
background 1. Specify the background color
2. That is, the color of the point whose value is 0 in the bitmap
3. An empty string means transparent
bitmap Specifies the bitmap to display
disabledbackground Specifies the background color when the bitmap object state is DISABLED
disabledbitmap Specifies the bitmap to be filled when the bitmap object state is DISABLED
disabledforeground Specifies the foreground color when the state of the bitmap object is DISABLED
foreground 1. Specify the foreground color
2. That is, the color of the point with a value of 1 in the bitmap
state 1. Specifies the state of the canvas object
2. Can be NORMAL, DISABLED (unavailable, does not respond to events) and HIDDEN (hidden)
3. The default value is NORMAL
tags Add a label to the created bitmap object
18.2.15.1 activebackground
The background color of the created bitmap when the mouse passes over it.
import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Canvas(root)
b1.create_bitmap(50,50,bitmap='question',
                 activebackground='red')
b1.pack()
root.mainloop()

Result:
insert image description here
18.2.15.2 activebitmap
The bitmap displayed when the mouse passes by.

import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Canvas(root)
b1.create_bitmap(50,50,bitmap='question',
                 activebitmap='info')
b1.pack()
root.mainloop()

Result:
insert image description here
Description: activebitmap is superimposed on the bitmap specified by bitmap.
18.2.15.3 activeforeground
The foreground color when the mouse passes over the bitmap.

import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Canvas(root)
b1.create_bitmap(50,50,bitmap='question',
                 activeforeground='red')
b1.pack()

root.mainloop()

Result:
insert image description here
18.2.15.4
How to place the bitmap at the specified position of the anchor. The default is centered. That is, the center of the bitmap is at the specified coordinates (x, y). Other option values ​​are: N, E, W, S, NE, NW, SE, SW, etc. NEWS stands for North, East, West and South respectively. Note that the position here refers to the position of the coordinate point relative to the center of the bitmap. In other words, E represents that the coordinate point is on the east side of the bitmap.

import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Canvas(root)
b1.create_bitmap(150,150,bitmap='@x.xbm',
                 anchor='n',activeforeground='red')
b1.create_bitmap(150,150,bitmap='@x.xbm',
                 anchor='e',activeforeground='blue')
b1.create_bitmap(150,150,bitmap='@x.xbm',
                 activeforeground='yellow')
b1.pack()
root.mainloop()

Result:
insert image description here
insert image description here
insert image description here
Explanation: The positioning methods of the three bitmaps are different, and the positions where they appear are also different.
18.2.15.5 background
The background color of the bitmap.

import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Canvas(root)
b1.create_bitmap(160,120,bitmap='@x.xbm',
                 background='yellow')
b1.pack()
root.mainloop()

Result:
insert image description here
18.2.15.6 bitmap
specifies a bitmap to display. There are two types of bitmaps in Python, which are:
(1) The internal btimap
uses the built-in bitmap, as long as the corresponding bitmap name is assigned to the bitmap parameter. Such as bitmap='error' and so on.
The names of the built-in bitmaps are:
'error', 'gray75', 'gray50', 'gray25', 'gray12', 'hourglass', 'info', 'questhead', 'question' and 'warning'. The specific pattern is shown in the figure below.
insert image description here
All files conforming to the xbm format can be displayed. The method is to add '@' before the file path. For example, the method of using the x.xbm file placed in the root directory of the C drive is:

import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Canvas(root)
b1.create_bitmap(160,120,bitmap='@c:\\x.xbm')
b1.pack()
root.mainloop()

Result:
insert image description here
18.2.15.7 disabledbackground
set the background color of bitmap state=tk.DISABLED. See the description of activebackground.
18.2.15.8 disabledbitmap The
bitmap when the state of bitmap=tk.DISABLED. Note that at this time, only the bitmap defined by disabledbitmap is displayed, but the bitmap specified by bitmap is not displayed.
18.2.15.9 disabledforeground
The foreground color when the state=tk.DISABLED of the bitmap. See the description of activeforeground.

import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
def disabled():
    b1.itemconfigure(bitmap,state='disabled')
b2=tk.Button(root,text='Disabled',command=disabled)
b2.pack()
b1=tk.Canvas(root)
bitmap=b1.create_bitmap(160,120,bitmap='@c:\\x.xbm',
                        disabledforeground='red')
b1.pack()
root.mainloop()

Result:
insert image description here
insert image description here
18.2.15.10 foreground
specifies the foreground color of the bitmap.

import tkinter as tk
root=tk.Tk()
root.geometry('320x240')
b1=tk.Canvas(root)
bitmap=b1.create_bitmap(160,120,bitmap='@x.xbm',
                        foreground='red')
b1.pack()
root.mainloop()

Result:
insert image description here
18.2.15.11 state
specifies the state of the bitmap. There are three types:
(1) NORMAL. Normal state, visible, optional, bound functions can be called
(2) DISABLED. Forbidden state, visible, but not selectable, the bound callback function does not work
(3) HIDDEN. hidden state. Invisible.
18.2.15.12 tags
specifies the bitmap tag, which is equivalent to an alias. You can also add tags in other ways

Guess you like

Origin blog.csdn.net/weixin_42272768/article/details/100852156