Python Forum
String int confused - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: String int confused (/thread-40451.html)



String int confused - janeik - Jul-30-2023

ny help to get the proper syntax regarding zoom. (great confusion in tha family).
Anyone?
lblzoomlvl = customtkinter.CTkLabel(window, width=80, font=("Ariel",16,"bold"),text_color="#0000ff", fg_color="#ffffff", text="")
lblzoomlvl.place(x=1160,y=241)

def cmdinczoom(event):
    #read Text from label lblzoomlvl into an integer variable zoom
    temp = lblzoomlvl["text"]
    zoom =int(temp)
    
    if (zoom<16):
        # add 1 to int variable zoom
        zoom=zoom+1
        # update text on label lblzoomlvl 
        lblzoomlvl.configure(text=zoom)
        # set mouse x,y position
        mouse.position = (32,53)
        # click once with left mouse button
        mouse.click(Button.left,1)
        # update text on lblzzoom by zoom
        lblzoomlvl.configure(text=zoom)
    
btninczoom = customtkinter.CTkButton(window, width=20, height=10, font=("Ariel",20,"bold"), border_color="#add19e", fg_color="#aaaaaa",text_color="#000000", text="+",command=cmdinczoom)
lblzoomlvl.[output]Tk.bind("<Up>",cmdinczoom)
  File "D:\Python311\Lib\tkinter\__init__.py", line 1448, in bind
    return self._bind(('bind', self._w), sequence, func, add)
           ^^^^^^^^^^
AttributeError: 'str' object has no attribute '_bind'. Did you mean: 'find'?
PS C:\Users\jan-e> & D:/Python311/python.exe d:/Python311/Scripts/nye.py
PS C:\Users\jan-e> & D:/Python311/python.exe d:/Python311/Scripts/nye.py
PS C:\Users\jan-e> & D:/Python311/python.exe d:/Python311/Scripts/nye.py
PS C:\Users\jan-e> [/output]bind("<Up>",cmdinczoom)



RE: String int confused - Gribouillis - Jul-30-2023

Try somewidget.bind("<Up>",cmdinczoom) perhaps.


RE: String int confused - deanhystad - Jul-31-2023

There is a better way to associate label text with an int. Use a tk.variable.
import tkinter as tk


def cmdinczoom(event):
    print(event)
    increment = {"Up": 1, "Right": 1, "Down": -1, "Left": -1}.get(event.keysym, 0)
    zoom.set(max(1, min(zoom.get() + increment, 16)))


window = tk.Tk()
zoom = tk.IntVar(window, 1)
label = tk.Label(window, width=10, font=("Ariel", 16, "bold"), textvariable=zoom)
label.pack(padx=20, pady=20)
for key in ("<Left>", "<Right>", "<Up>", "<Down>"):
    window.bind(key, cmdinczoom)

window.mainloop()
Why does your key press callback function press a mouse button?


RE: String int confused - janeik - Jul-31-2023

(Jul-31-2023, 03:15 AM)deanhystad Wrote: There is a better way to associate label text with an int. Use a tk.variable.
import tkinter as tk


def cmdinczoom(event):
    print(event)
    increment = {"Up": 1, "Right": 1, "Down": -1, "Left": -1}.get(event.keysym, 0)
    zoom.set(max(1, min(zoom.get() + increment, 16)))


window = tk.Tk()
zoom = tk.IntVar(window, 1)
label = tk.Label(window, width=10, font=("Ariel", 16, "bold"), textvariable=zoom)
label.pack(padx=20, pady=20)
for key in ("<Left>", "<Right>", "<Up>", "<Down>"):
    window.bind(key, cmdinczoom)

window.mainloop()
Why does your key press callback function press a mouse button?

hello. I am using [url=https://github.com/TomSchimansky/TkinterMapView#create-polygon-from-position-
list]https://github.com/TomSchimansky/TkinterMapView#create-polygon-from-position-list[/url]



I wanted to keep track of zoom level on his app, view lvl on a label and use up and down arrow on keyboard to increase/decrease zoom level 1-16.

I didnt find any direct function in Schimansky app, so I thought I should do it programmatically


RE: String int confused - janeik - Jul-31-2023

I did consentrate on the arrow up and this solved it:

def pilopp(event):
    tekst = lblzoomlvl.cget("text")
    print(tekst)
    if (int(tekst)<16):
        # set mouse x,y position
        mouse.position = (32,53)
        mouse.click(Button.left,1)
        tekst = int(tekst) + 1
        lblzoomlvl.configure(text=tekst)
window.bind("<Up>",pilopp)



RE: String int confused - deanhystad - Aug-01-2023

Why are you setting the mouse position and calling mouse click? Is that supposed to press the zoom button? zoom and set_zoom() are part of the map api, why not use them? You've posted code that uses the set_zoom() function.

Below is something I posted before with the addition of binding the arrow keys to zoom in and out.
import tkinter as tk
from tkintermapview import TkinterMapView


class MyWindow(tk.Tk):
    """A window that selects one of several pages to display."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.map = TkinterMapView(self, width=800, height=600, corner_radius=0)
        self.map.pack(expand=True, fill=tk.BOTH)
        self.map.set_position(60.03345, 11.35806)  # Norway
        self.map.set_address("fjellvegen 4, auli, norway", marker=True)
        self.map.set_zoom(16)
        self.zoom_label = tk.Label(self, text="Zoom = 16", font=(None, 24), width=20)
        self.zoom_label.pack(padx=10, pady=10)

        for key in ("<Left>", "<Right>", "<Up>", "<Down>"):
            self.bind(key, self.zoom)

    def zoom(self, event):
        increment = {"Up": 1, "Right": 1, "Down": -1, "Left": -1}.get(event.keysym, 0)
        value = max(1, min(19, self.map.zoom + increment))
        self.zoom_label["text"] = f"Zoom = {value}"
        self.map.set_zoom(value)


MyWindow().mainloop()
Quote:I wanted to keep track of zoom level on his app
The best way to "keep track" of the zoom level is ask the map what the current zoom level is. There are too many ways this can change. You can change the zoom programmatically. You can change the zoom by hitting the zoom in (+) and out (-) buttons. You can zoom using the mouse wheel. There might be a way to have the map tell you when the zoom level changes. I didn't see it, but that doesn't mean it isn't there. If you want to display the current zoom level you could fire off an event that runs periodically to update the zoom display (use tkinter.after()). If you just want to know what the current zoom is, to save in a configuration file for example, just ask the map.zoom.


RE: String int confused - janeik - Aug-02-2023

(Aug-01-2023, 06:48 PM)deanhystad Wrote: Why are you setting the mouse position and calling mouse click? Is that supposed to press the zoom button? zoom and set_zoom() are part of the map api, why not use them? You've posted code that uses the set_zoom() function.

I wasnt aware of the api, just looked for a function which could possibly inc/dec zoom of the map. So I use the maps own zoom buttons instead. Thats not a good solution. Else I cut out the -/+ buttons and use keyboard down/up instead and plan to block the third mousewheel info as its dependant of mouse config. Do You mind post link to api?

Below is something I posted before with the addition of binding the arrow keys to zoom in and out.
import tkinter as tk
from tkintermapview import TkinterMapView


class MyWindow(tk.Tk):
    """A window that selects one of several pages to display."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.map = TkinterMapView(self, width=800, height=600, corner_radius=0)
        self.map.pack(expand=True, fill=tk.BOTH)
        self.map.set_position(60.03345, 11.35806)  # Norway
        self.map.set_address("fjellvegen 4, auli, norway", marker=True)
        self.map.set_zoom(16)
        self.zoom_label = tk.Label(self, text="Zoom = 16", font=(None, 24), width=20)
        self.zoom_label.pack(padx=10, pady=10)

        for key in ("<Left>", "<Right>", "<Up>", "<Down>"):
            self.bind(key, self.zoom)

    def zoom(self, event):
        increment = {"Up": 1, "Right": 1, "Down": -1, "Left": -1}.get(event.keysym, 0)
        value = max(1, min(19, self.map.zoom + increment))
        self.zoom_label["text"] = f"Zoom = {value}"
        self.map.set_zoom(value)
Your code is most elegant :) 

MyWindow().mainloop()
Quote:I wanted to keep track of zoom level on his app
The best way to "keep track" of the zoom level is ask the map what the current zoom level is. There are too many ways this can change. You can change the zoom programmatically. You can change the zoom by hitting the zoom in (+) and out (-) buttons. You can zoom using the mouse wheel. There might be a way to have the map tell you when the zoom level changes. I didn't see it, but that doesn't mean it isn't there. If you want to display the current zoom level you could fire off an event that runs periodically to update the zoom display (use tkinter.after()). If you just want to know what the current zoom is, to save in a configuration file for example, just ask the map.zoom.

Buttons already cuttoff. Mousewheel 3 will too, if possible. Just need the zoom for later rewinding of maparea and its marks/icons/symbols.


RE: String int confused - deanhystad - Aug-02-2023

This does save and restore.
import tkinter as tk
from tkintermapview import TkinterMapView


class MyWindow(tk.Tk):
    """A window that displays a  map."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.map = TkinterMapView(self, width=800, height=600, corner_radius=0)
        self.map.pack(expand=True, fill=tk.BOTH)
        self.map.set_position(60.03345, 11.35806)  # Norway
        self.map.set_address("fjellvegen 4, auli, norway", marker=True)
        self.map.set_zoom(16)
        self.save()

        # Create save and restore buttons for saving/restoring map position and zoom.
        frame = tk.Frame(self)
        frame.pack(expand=True, fill=tk.X)
        button = tk.Button(frame, text="Save", command=self.save, font=(None, 24))
        button.pack(side=tk.LEFT, expand=True, fill=tk.X)
        button = tk.Button(frame, text="Restore", command=self.restore, font=(None, 24))
        button.pack(side=tk.LEFT, expand=True, fill=tk.X)

        # Bind arrow keys to zooming in/out.
        self.zoom_keys = {"Up": 1, "Right": 1, "Down": -1, "Left": -1}
        for key in self.zoom_keys:
            self.bind(f"<{key}>", self.arrow_zoom)

    def arrow_zoom(self, event):
        """Zoom map using arrow keys."""
        increment = self.zoom_keys.get(event.keysym, 0)
        self.map.set_zoom(self.map.zoom + increment)

    def save(self):
        """Save zoom and position so map can be restored later."""
        self.saved_info = (self.map.zoom, self.map.get_position())

    def restore(self):
        """Restore map to saved zoom and position."""
        zoom, position = self.saved_info
        self.map.set_zoom(zoom)
        self.map.set_position(*position)


MyWindow().mainloop()