英文:
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('System')
set_default_color_theme('dark-blue')
window = CTk() # Window
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() # 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
画布上有名为 canvasx
和 canvasy
的方法,它们将窗口坐标转换为画布坐标。
您的绘图函数应该如下所示:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论