如何防止画布中的背景滚动? [英] How to prevent background scrolling in canvas?

查看:299
本文介绍了如何防止画布中的背景滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ttk.Scrollbar滚动Tkinter.Canvas中的元素。不幸的是,这个画布的背景图像也会滚动。如何防止这种情况?



以下是我使用的示例:

 从ttk import导入Tkinter为tk 
*

#带滚动条的第一个画布
root = tk.Tk()
canv1 = tk.Canvas( root,width = 200,height = 200,bg ='green')
sbar = Scrollbar(root,orient =vertical)
sbar.config(command = canv1.yview)
sbar.pack(side =right,fill =y)
canv1.config(yscrollcommand = sbar.set)
canv1.pack(side =right,fill =y)

#dirst canvas的背景
photo_content2 = tk.PhotoImage(file ='。/ sample_image.gif')
canv1.create_image(115,300,image = photo_content2)

#scrollable second canvas insode first
canv2 = tk.Canvas(canv1,width = 50,height = 30,bg ='red')
canv2.pack()
canv1.create_window(20,20,window = canv2)
canv1.config(scrollregion =(0,0,300,1000))

root.mainloop()


解决方案

滚动影响每一个人画在画布上。你不能添加不与其他所有内容一起滚动的背景图像。



话虽如此,你可以创建一个在滚动后移动背景图像的函数发生了,你得到的效果就像图像没有滚动一样。



您可以通过将滚动条连接到自定义函数而不是直接连接到小部件来执行此操作 yview 和/或 xview 命令。在此功能中,您可以正常调用 yview xview 命令,然后重新定位背景图像。 / p>

例如,这就是你如何处理垂直滚动;水平滚动可以以类似的方式工作:

  def custom_yview(* args,** kwargs):
canvas。 yview(* args,** kwargs)
x = canvas.canvasx(0)
y = canvas.canvasy(0)
canvas.coords(background,x,y)

canvas = tk.Canvas(...)
vsb = tk.Scrollbar(orient =vertical,command = custom_yview)
canvas.create_image(...,tags =( background,))


I am using ttk.Scrollbar to scroll elements inside a Tkinter.Canvas. Unfortunately a background image of this canvas will also scroll. How can I prevent this?

Here is an example of what I use:

import Tkinter as tk
from ttk import *

# first canvas with scrollbar
root = tk.Tk()
canv1 = tk.Canvas(root, width=200, height=200, bg='green')
sbar = Scrollbar(root, orient="vertical")
sbar.config(command=canv1.yview)
sbar.pack(side="right", fill="y")      
canv1.config(yscrollcommand=sbar.set)
canv1.pack(side="right", fill="y")

# background of dirst canvas
photo_content2 = tk.PhotoImage(file = './sample_image.gif')
canv1.create_image(115,300, image=photo_content2)

# scrollable second canvas insode first
canv2 = tk.Canvas(canv1, width=50, height=30, bg='red')
canv2.pack()
canv1.create_window(20,20, window=canv2)
canv1.config(scrollregion=(0,0,300,1000))

root.mainloop()

解决方案

Scrolling affects everything drawn on the canvas. You can't add a background image that doesn't scroll along with everything else.

That being said, you can create a function that moves the background image after the scrolling happens, and you get the same effect as if the image didn't scroll.

You do this by connecting the scrollbar to a custom function rather than directly to the widgets yview and/or xview commands. In this function you can call the yview and xview commands as normal, and then re-position the background image.

For example, this is how you would handle vertical scrolling; horizontal scrolling would work in a similar way:

def custom_yview(*args, **kwargs):
    canvas.yview(*args, **kwargs)
    x = canvas.canvasx(0)
    y = canvas.canvasy(0)
    canvas.coords("background", x,y)

canvas = tk.Canvas(...)
vsb = tk.Scrollbar(orient="vertical", command=custom_yview)
canvas.create_image(..., tags=("background,"))

这篇关于如何防止画布中的背景滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆