Hello,
here is my code:

import tkinter as tk
import tkinter.font as tkFont

from tkinter import ttk
from tkinter import *

root = tk.Tk()
pressed = False

class Example(tk.Frame):
    def _create_circle(self, x, y, r, **kwargs):
        return self.create_oval(x-r, y-r, x+r, y+r, **kwargs)
    tk.Canvas.create_circle = _create_circle

    def __init__(self, root):
        tk.Frame.__init__(self, root)
        self.customFont = tkFont.Font(family="Helvetica", size=20)
        root.title("Test")
        self.canvas = Canvas(self, width=400, height=400, background="white")
        self.xsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        self.ysb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.ysb.set, xscrollcommand=self.xsb.set)
        self.canvas.configure(scrollregion=(0,0,1000,1000))

        self.xsb.grid(row=1, column=0, sticky="ew")
        self.ysb.grid(row=0, column=1, sticky="ns")
        self.canvas.grid(row=0, column=0, sticky="nsew")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.canvas.create_line(300,160,1200,160, width = 10, fill= 'black')
        self.canvas.create_line(300,200,1200,200, width = 10, fill= 'black')

        self.canvas.create_circle(278, 180, 30, width = 10)

        FONT_NAME = "Helvetica"
        self.canvas.create_text(380, 157, font= (FONT_NAME,5), text = 'Text1', fill = 'Gray')
        self.canvas.create_text(380, 159, font= (FONT_NAME,5), text = 'Text2', fill = 'Red')
        self.canvas.create_text(380, 197, font= (FONT_NAME,5), text = 'Text3', fill = 'Gray')

        self.canvas.bind("<ButtonPress-1>", self.move_start)
        self.canvas.bind("<B1-Motion>", self.move_move)

        self.canvas.bind("<ButtonPress-2>", self.pressed2)
        self.canvas.bind("<Motion>", self.move_move2)

        self.canvas.bind("<Button-4>", self.zoomerP)
        self.canvas.bind("<Button-5>", self.zoomerM)

        self.canvas.bind("<MouseWheel>",self.zoomer)

        root.bind_all("<MouseWheel>",self.zoomer)

    def move_start(self, event):
        self.canvas.scan_mark(event.x, event.y)
    def move_move(self, event):
        self.canvas.scan_dragto(event.x, event.y, gain=1)

    def pressed2(self, event):
        global pressed
        pressed = not pressed
        self.canvas.scan_mark(event.x, event.y)
    def move_move2(self, event):
        if pressed:   
            self.canvas.scan_dragto(event.x, event.y, gain=1)

    def zoomer(self,event):        
        if (event.delta > 0):
            self.canvas.scale('all', event.x, event.y, 1.1, 1.1)
            self.canvas.configure(scrollregion = self.canvas.bbox("all"))

        elif (event.delta < 0):
            self.canvas.scale('all', event.x, event.y, 0.9, 0.9)
            self.canvas.configure(scrollregion = self.canvas.bbox("all"))

    def zoomerP(self,event):
        self.canvas.scale("all", event.x, event.y, 1.1, 1.1)
        self.canvas.configure(scrollregion = self.canvas.bbox("all"))

    def zoomerM(self,event):
        self.canvas.scale("all", event.x, event.y, 0.9, 0.9)
        self.canvas.configure(scrollregion = self.canvas.bbox("all"))

if __name__ == "__main__":
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

I have two questions:

  1. How do I set the minimum and maximum value for the mousewheel(setting the zoom in and zoom out range).
  2. I am getting a Error as UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte in both the code. How do I resolve it.

I would greatly appreciate any help or suggestions.

Thank you.

Recommended Answers

All 3 Replies

I don't see much in the way of comments but let's say that x and y are the values that drive the drawing. Given that won't you test that that x and y don't exceed the drawing bonderies before you draw? Something like:

if x>xmax x=xmax ' Test and constrain x, y.
if y>ymax y=ymax

I have already tried that but the mouse wheel is not accepting any changes to it. I would greatly appreciate it if you can provide a example.

Thank you.

Your reply writes about mouse wheel not accepting changes which isn't my thought about how to constrain values in code.

Maybe you are thinking you need to change what values come from the mouse rather than using the mouse values to change your app values and then test for and constrain the value in your app to fit your needs. In other words, you need to think about this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.