制作帆布拖拽

2024-10-01 15:45:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个涉及在画布上生成RawTurtle的项目。我在想如果画出了屏幕我会怎么做。有谁能告诉我如何使Tkinter画布小部件可拖动?在

from tkinter import *
root = Tk()
c = Canvas(root)
t = RawTurtle(c)

....# What can i do to have a drag function
root.mainloop()

Tags: 项目fromimport屏幕部件tkinter画布root
1条回答
网友
1楼 · 发布于 2024-10-01 15:45:02

这是Python3的一个答案,但是更改导入,它应该可以很好地与Python2一起工作

#!python3

import tkinter as tk
import turtle

def run_turtles(*args):
    for t, d in args:
        t.circle(200, d)
    root.after_idle(run_turtles, *args)

def scroll_start(event):
    screen.scan_mark(event.x, event.y)

def scroll_move(event):
    screen.scan_dragto(event.x, event.y, gain=1)

root = tk.Tk()
root.geometry("700x700")
root.withdraw()

frame = tk.Frame(bg='black')
frame.pack(fill='both', expand=True)
tk.Label(frame, text=u'Hello', bg='grey', fg='white').pack(fill='x')

screen = turtle.ScrolledCanvas(frame)
screen.pack(fill="both", expand=True)


turtle1 = turtle.RawTurtle(screen)
turtle2 = turtle.RawTurtle(screen)

screen.bind("<ButtonPress-1>", scroll_start)
screen.bind("<B1-Motion>", scroll_move)

turtle1.ht(); turtle1.pu()
turtle1.left(90); turtle1.fd(200); turtle1.lt(90)
turtle1.st(); turtle1.pd()

turtle2.ht(); turtle2.pu()
turtle2.fd(200); turtle2.lt(90)
turtle2.st(); turtle2.pd()

root.deiconify()

run_turtles((turtle1, 3), (turtle2, 4))

root.mainloop()

值得注意的是:

  1. 由于某些原因,一旦添加了海龟,画布绑定将停止工作,最好的解决方法是在添加海龟之后再添加绑定。或者,可以绑定到顶部窗口。在
  2. 画布只能平移到滚动区域的边缘,如果要进一步平移,则需要将其放大。在

相关问题 更多 >

    热门问题