使用乌龟和单击/双击实现 Python tkinter 缩放 [英] Implement Python tkinter zoom with turtle and single/double click

查看:59
本文介绍了使用乌龟和单击/双击实现 Python tkinter 缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在画布中绘制海龟,并且我还尝试通过单击和双击事件实现放大缩小功能.当我不尝试实现 tkinter 时,代码工作得很好,但是当我尝试执行放大缩小功能时,我无法执行它.如果您有任何建议或帮助,我将不胜感激.

I am trying to draw turtle in canvas and I am also trying to implement zoom in zoom out feature with single click and double click event. When I am not trying to implement tkinter the code works absolutely fine but when I am trying to perform zoom in zoom out feature, I am unable to execute it. I would greatly appreciate any suggestions or help.

这是我的代码:

import turtle
import tkinter as tk
from tkinter import *

root = tk.Tk()
canvas = tk.Canvas(master = root, width = 2700, height = 2500)
canvas.pack(fill=BOTH, expand=1)
mb = Menubutton(None, text='Mouse Clicks')
mb.pack()
t = turtle.RawTurtle(canvas)

def parallel():
window= canvas

def zoomin(event):
    d = event.delta
    if d < 0:
       amt=0.9
    else:
       amt=1.1

    canvas.scale(ALL, 2700,2500 , amt,amt)
    mb.bind('<Button-1>', zoomin)

    def zoomout(event1, d1, amt1):
        d1 = event1.delta
        if d1 >0:
           amt1=1.1
        else:
           amt1=0.7
    canvas.scale(ALL, 2700,2500 , amt, amt)
    mb.bind('<Double-1>', zoomout)

    t.pu()
    t.left(90)
    t.forward(70)
    t.rt(90)
    t.pd()
    t.width(8)
    t.color("LightGray")
    t.forward(1200)
    t.back(1200)
    t.pu()
    t.left(90)
    t.forward(25)
    t.rt(90)
    t.pd()
    t.forward(1200)
    t.back(1200)

    t.pu()
    t.setposition(-85, 45)
    t.pd()
    t.forward(80)
    t.left(90)
    t.forward(80)
    t.left(90)
    t.forward(80)
    t.left(90)
    t.forward(80)
    t.left(90)

    t.penup()                
    t.goto(-200, 160)       
    t.write("Class", True, align="center", font=('TimesNewRoman', 20, 'normal'))
    t.pendown()              
    t.penup()
    t.goto(-45, 150)
    t.write("1", True, align="center", font=('TimesNewRoman', 50, 'normal'))
    t.pendown()

    parallel() 
    t.mainloop()

谢谢.

推荐答案

您的程序似乎不完整且结构不正确.以下是我认为您正在尝试做的极简示例.它绘制一个圆圈,然后让您使用鼠标滚轮放大和缩小:

Your program seems incomplete and structured incorrectly. Below is my minimalist example of what I believe you're trying to do. It draws a circle and then lets you use the mouse wheel to zoom it in and out:

import tkinter as tk
from turtle import RawTurtle, TurtleScreen, ScrolledCanvas

def zoom(event):
    amount = 0.9 if event.delta < 0 else 1.1

    canvas.scale(tk.ALL, 0, 0, amount, amount)

root = tk.Tk()

canvas = ScrolledCanvas(master=root, width=2000, height=2000)
canvas.pack(fill=tk.BOTH, expand=tk.YES)

screen = TurtleScreen(canvas)

turtle = RawTurtle(screen)

turtle.penup()
turtle.sety(-250)
turtle.pendown()
turtle.circle(250)

canvas.bind('<MouseWheel>', zoom)

screen.mainloop()

请注意,某些海龟元素,例如 dot() 和使用 write() 生成的文本不会缩放,它们将保持固定.您需要阅读 .scale() 方法,并深入了解 tkinter,以可能解决此问题.或者自己手动缩放字体.

Note that certain turtle elements, like dot() and text generated with write() won't zoom, they'll remain fixed. You'll need to read about the .scale() method, and go deeper into tkinter, to potentially work around this. Or manually scale your fonts yourself.

这篇关于使用乌龟和单击/双击实现 Python tkinter 缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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