Python Tkinter canvas.xview units
How are the 'units' (what
) from the Tkinter canvas scrolling methods xview(SCROLL, step, what)
and yview(SCROLL, step, what)
defined? Is it defined 开发者_如何学JAVAin pixels? Is it possible to change it (for a slower scrolling for example)?
For a better context please see the code here.
Thanks in advance.
for slower scrolling, you can play around with the xscrollincrement & yscrollincrement options of the Canvas:
from Tkinter import *
root = Tk()
c = Canvas(root, scrollregion=(0,0,500,500), height=200, width=200)
s = Scrollbar(root, command=c.yview)
c.pack(side=LEFT)
s.pack(side=RIGHT, fill=Y)
c.configure(yscrollcommand=s.set)
c.configure(yscrollincrement='2')
##yscrollincrement - increment for vertical scrolling, in pixels,
##millimeters '2m', centimeters '2c', or inches '2i'
c.create_rectangle(10,10,100,100)
c.create_rectangle(10,200,100,300)
def rollWheel(event):
direction = 0
if event.num == 5 or event.delta == -120:
direction = 1
if event.num == 4 or event.delta == 120:
direction = -1
event.widget.yview_scroll(direction, UNITS)
c.bind('<MouseWheel>', lambda event: rollWheel(event))
c.bind('<Button-4>', lambda event: rollWheel(event))
c.bind('<Button-5>', lambda event: rollWheel(event))
c.focus_set()
root.mainloop()
Have a look at the docs for the -xview/-yview options, especially at the the yscrollincrement option too. So yes, you can change the step size.
-yscrollincrement
Specifies an increment for vertical scrolling, in any of the usual forms permitted for screen distances. If the value of this option is greater than zero, the vertical view in the window will be constrained so that the canvas y coordinate at the top edge of the window is always an even multiple of yScrollIncrement; furthermore, the units for scrolling (e.g., the change in view when the top and bottom arrows of a scrollbar are selected) will also be yScrollIncrement. If the value of this option is less than or equal to zero, then vertical scrolling is unconstrained.
and
yview scroll number what
This command adjusts the view in the window up or down according to number and what. Number must be an integer. What must be either units or pages. If what is units, the view adjusts up or down in units of the yScrollIncrement option, if it is greater than zero, or in units of one-tenth the window's height otherwise. If what is pages then the view adjusts in units of nine-tenths the window's height. If number is negative then higher information becomes visible; if it is positive then lower information becomes visible.
from the Tk manpage for canvas.
精彩评论