如何在使用滚动条向下滚动后获取 tkinter 画布的正确位置?

huangapple go评论67阅读模式
英文:

How can I get the correct position of a tkinter canvas after scrolling down using scrollbar?

问题

我正在使用customtkinter在Python中制作绘画工具。我有一个可滚动的画布,当我点击画布时,它根据光标位置绘制一个椭圆。但是,当我向下滚动并尝试绘制东西时,它绘制的位置不正确。

这是我的代码:

from customtkinter import *
from tkinter import Scrollbar

set_appearance_mode('System')
set_default_color_theme('dark-blue')

window = CTk()  # 窗口
window.geometry("800x520")

fill_color = '#000000'

def draw_with_brush(event):
    fx, fy = event.x - 1, event.y - 1
    lx, ly = fx + 1, fy + 1
    canvas.create_oval((fx, fy, lx, ly), outline=fill_color, width=10, fill=fill_color)

canvas = CTkCanvas(window, bg='white', bd=2, relief=SUNKEN, height=430, width=700, scrollregion=(0, 0, 100000, 100000))
canvas.place(x=60, y=50)
canvas.bind("<B1-Motion>", draw_with_brush)

x_scroll = Scrollbar(window, orient=HORIZONTAL, command=canvas.xview)
x_scroll.place(x=60, y=495, width=700)
y_scroll = Scrollbar(window, orient=VERTICAL, command=canvas.yview)
y_scroll.place(x=780, y=50, height=430)

canvas.config(xscrollcommand=x_scroll.set, yscrollcommand=y_scroll.set)

window.mainloop()  # 循环

我理解我遇到这个问题是因为当我向下滚动时,它不会更改椭圆的位置。但我不知道任何方法可以告诉我滚动后画布的位置发生了什么变化。

我尝试使用函数Canvas.canvasx()Canvas.canvasy(),但得到的结果完全相同。

英文:

I'm making paint in Python using customtkinter. I have a scrollable canvas and when I click on canvas it draws an oval using cursor position. But when I'm scrolling down and trying to draw something, it draws everything not in the right position.

Here's my code:

from customtkinter import *
from tkinter import Scrollbar

set_appearance_mode(&#39;System&#39;)
set_default_color_theme(&#39;dark-blue&#39;)

window = CTk()  # Window
window.geometry(&quot;800x520&quot;)

fill_color = &#39;#000000&#39;

def draw_with_brush(event):
    fx, fy = event.x - 1 , event.y - 1
    lx, ly = fx + 1, fy + 1
    canvas.create_oval((fx, fy, lx, ly), outline=fill_color, width=10, fill=fill_color)

canvas = CTkCanvas(window, bg=&#39;white&#39;, bd=2, relief=SUNKEN, height=430, width=700, scrollregion=(0,0,100000,100000))
canvas.place(x=60, y=50)
canvas.bind(&quot;&lt;B1-Motion&gt;&quot;, draw_with_brush)

x_scroll = Scrollbar(window, orient=HORIZONTAL, command=canvas.xview)
x_scroll.place(x=60, y=495, width=700)
y_scroll = Scrollbar(window, orient=VERTICAL, command=canvas.yview)
y_scroll.place(x=780, y=50, height=430)

canvas.config(xscrollcommand=x_scroll.set, yscrollcommand=y_scroll.set)

window.mainloop()  # Loop

I understand that I have this problem because I'm scrolling down and this don't make any changes to my position where oval will be. But I don't know any method which will say me how position of canvas changed after I scrolled down.

I tried using functions Canvas.canvasx() and Canvas.canvasy() but I got exactly the same result.

答案1

得分: 1

画布上有名为 canvasxcanvasy 的方法,它们将窗口坐标转换为画布坐标。

您的绘图函数应该如下所示:

def draw_with_brush(event):
    x = event.widget.canvasx(event.x)
    y = event.widget.canvasy(event.y)
    fx, fy = x - 1 , y - 1
    lx, ly = fx + 1, fy + 1
    event.widget.create_oval((fx, fy, lx, ly), outline=fill_color, width=10, fill=fill_color)
英文:

The canvas has methods named canvasx and canvasy which will convert window coordinates to canvas coordinates.

Your drawing function should look like this:

def draw_with_brush(event):
    x = event.widget.canvasx(event.x)
    y = event.widget.canvasy(event.y)
    fx, fy = x - 1 , y - 1
    lx, ly = fx + 1, fy + 1
    event.widget.create_oval((fx, fy, lx, ly), outline=fill_color, width=10, fill=fill_color)

huangapple
  • 本文由 发表于 2023年5月22日 20:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定