[Tutor] Tkinter Scale Question

Sean Finnegan vinnievega007 at gmail.com
Sun Dec 5 20:21:28 CET 2010


Hi Everyone,

I have successfully visualized 4 layers in Tkinter and am now trying to
apply some sort of zoom in/out and pan function so that the layers can be
seen more closely. I am using a package to visualize the map layers where i
have separate py files for points, polylines, polygons, map, layers,
readshapefile, feature, and features, and a main file that tells tkinter
what to draw based on the bounding box settings in the map file. I am not
exactly sure what path I should be exploring, either a button module or a
scale module. I have successfully visualized both, but can not seem to make
them take on the functionality that I need.

Most recently I am trying some bind code in the main file that I have
modified to try and fit the map needs, but it isn't producing the desired
functionality either. Can someone please give me some direction as to which
way would be best to approach this?


from Tkinter import *
from Map import *
root = Tk()

import sys
if sys.version_info < (3,):
    import Tkinter as tk
    import tkSimpleDialog as tk_dialog
else:
    import tkinter as tk
    from tkinter import simpledialog as tk_dialog

class Viewer(object):
    #Base class viewer to display map

    def __init__(self, parent, width=800, height=600,
                 min_x=-2.5, min_y=-1.5, max_x=1.):

        self.parent = parent
        self.canvas_width = width
        self.canvas_height = height

        # The following are drawing boundaries in the complex plane
        self.min_x = min_x
        self.min_y = min_y
        self.max_x = max_x
        self.calculate_pixel_size()
        self.max_y = self.min_y + self.canvas_height*self.pixel_size

        self.calculating = False
        self.nb_iterations = 20
        self.normal_zoom(None)

        self.canvas = tk.Canvas(parent, width=width, height=height)
        self.canvas.pack()
        self.status = tk.Label(self.parent, text="", bd=1, relief=tk.SUNKEN,
                               anchor=tk.W)
        self.status.pack(side=tk.BOTTOM, fill=tk.X)
        self.status2 = tk.Label(self.parent, text=self.info(), bd=1,
                                relief=tk.SUNKEN, anchor=tk.W)
        self.status2.pack(side=tk.BOTTOM, fill=tk.X)

        # We change the size of the image using the keyboard.
        self.parent.bind("+", self.zoom_in)
        self.parent.bind("-", self.zoom_out)
        self.parent.bind("n", self.normal_zoom)
        self.parent.bind("b", self.bigger_zoom)

        # Set the maximum number of iterations via a keyboard-triggered
event
        self.parent.bind("i", self.set_max_iter)

        # We move the canvas using the mouse.
        self.translation_line = None
        self.parent.bind("<button-1>", self.mouse_down)
        self.parent.bind("<button1-motion>", self.mouse_motion)
        self.parent.bind("<button1-buttonrelease>", self.mouse_up)

        self.draw_map()  # Needs to be implemented by subclass

    def info(self):
        #information about map location'''
        return "Location: (%f, %f) to (%f, %f)" %(self.min_x, self.min_y,
                                                  self.max_x, self.max_y)

    def calculate_pixel_size(self):
        #Calculates the size of a (square) pixel in complex plane
        #coordinates based on the canvas_width.
        self.pixel_size = 1.*(self.max_x - self.min_x)/self.canvas_width
        return

    def mouse_down(self, event):
        #records the x and y positions of the mouse when the left button
           #is clicked.
        self.start_x = self.canvas.canvasx(event.x)
        self.start_y = self.canvas.canvasy(event.y)

    def mouse_motion(self, event):
        #keep track of the mouse motion by drawing a line from its
           #starting point to the current point.
        x = self.canvas.canvasx(event.x)
        y = self.canvas.canvasy(event.y)

        if (self.start_x != event.x)  and (self.start_y != event.y) :
            self.canvas.delete(self.translation_line)
            self.translation_line = self.canvas.create_line(
                                self.start_x, self.start_y, x, y,
fill="orange")
            self.canvas.update_idletasks()

    def mouse_up(self, event):
        #Moves the canvas based on the mouse motion
        dx = (self.start_x - event.x)*self.pixel_size
        dy = (self.start_y - event.y)*self.pixel_size
        self.min_x += dx
        self.max_x += dx
        # y-coordinate in complex plane run in opposite direction from
        # screen coordinates
        self.min_y -= dy
        self.max_y -= dy
        self.canvas.delete(self.translation_line)
        self.status.config(text="Moving the map.  Please wait.")
        self.status.update_idletasks()
        self.status2.config(text=self.info())
        self.status2.update_idletasks()
        self.draw_map()

    def normal_zoom(self, event, scale=1):
        #Sets the zooming in/out scale to its normal value
        if scale==1:
            self.zoom_info = "[normal zoom]"
        else:
            self.zoom_info = "[faster zoom]"
        if event is not None:
            self.status.config(text=self.zoom_info)
            self.status.update_idletasks()
        self.zoom_in_scale = 0.1
        self.zoom_out_scale = -0.125

    def bigger_zoom(self, event):
        #Increases the zooming in/out scale from its normal value
        self.normal_zoom(event, scale=3)
        self.zoom_in_scale = 0.3
        self.zoom_out_scale = -0.4

    def zoom_in(self, event):
        #decreases the size of the region of the complex plane displayed
        if self.calculating:
            return
        self.status.config(text="Zooming in.  Please wait.")
        self.status.update_idletasks()
        self.change_scale(self.zoom_in_scale)

    def zoom_out(self, event):
        #increases the size of the region of the complex plane displayed'''
        if self.calculating:
            return
        self.status.config(text="Zooming out.  Please wait.")
        self.status.update_idletasks()
        self.change_scale(self.zoom_out_scale)

    def change_scale(self, scale):
        #changes the size of the region of the complex plane displayed and
           #redraws
        if self.calculating:
            return
        dx = (self.max_x - self.min_x)*scale
        dy = (self.max_y - self.min_y)*scale
        self.min_x += dx
        self.max_x -= dx
        self.min_y += dy
        self.max_y -= dy
        self.calculate_pixel_size()
        self.draw_map()

    def set_max_iter(self, event):
        #set maximum number of iterations
        i = tk_dialog.askinteger('title', 'prompt')
        if i is not None:
            self.nb_iterations = i
            self.status.config(text="Redrawing.  Please wait.")
            self.status.update_idletasks()
            self.draw_map()

    def draw_map(self):
        #draws map on the canvas
        raise NotImplementedError
map = Map(root, 800, 600)
map.addLayer('States')
map.addLayer('USInterstates')
map.addLayer('Rivers')
map.addLayer('RoadsRiversIntersect_points')
map.vis()
root.mainloop()
#root.wait_window()

Thank you,
-Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101205/5d80b14d/attachment-0001.html>


More information about the Tutor mailing list