Python Forum
tkinter mapview in a frame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter mapview in a frame
#1
I have 3 frames ipage1, page2,page3 in app, starting at page2 when entering app.
Page2 runs tkinter mapview.
Entering paee1/page2 "kills" the mapview.
Is there a way to keep frame2 tkinter mapview, alive when returning from page1/page3?

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy_garden.mapview import MapView
from kivy_garden.mapview import MapView, MapMarkerPopup
from kivy.uix.button import Button
from tkinter import *
import tkinter
import tkintermapview
window = tkinter.Tk()
window.geometry(f"{800}x{600}")
window.title("map_view_example.py")
# window = Tk()
window.rowconfigure(0, weight=1)
window.columnconfigure(0, weight=1)
window.state("zoomed")
window.title("WINDOW 1")

page1 = Frame(window)
page2 = Frame(window)
page3 = Frame(window)

for frame in (page1, page2, page3):
    frame.grid(row=0, column=0, sticky="nsew")
    
def show_frame(frame):
    frame.tkraise()
show_frame(page2)

# ==============page1==============
page1.config(background="blue")
pag1_label = Label(page1, text="page1")
pag1_label.place(x=56, y=56)

pg2_button = Button(page1, text="NEXT", command = lambda: show_frame(page2))
pg2_button.place(x=1700, y=50)

#==================Page2==================

pag2_label = Label(page2, text="page2")
pag2_label.place(x=56, y=56)

map_widget = tkintermapview.TkinterMapView(window, width=800, height=600, corner_radius=0)
map_widget.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
# set current widget position and zoom
map_widget.set_position(60.03345, 11.35806)  # Norway
marker_1 = map_widget.set_address("fjellvegen 4, auli, norway", marker=True)
map_widget.set_zoom(16)

pg2_button = Button(page2, text="NEXT", command = lambda: show_frame(page3))
pg2_button.place(x=1700, y=50)

#==================Page3==================
page3.config(background="green")
pag3_label = Label(page3, text="page3")
pag3_label.place(x=56, y=56)
pg3_button = Button(page3, text="NEXT", command = lambda: show_frame(page1))
pg3_button.place(x=1700, y=50)

window.mainloop()
Reply
#2
map_widget = tkintermapview.TkinterMapView(window, width=800, height=600, corner_radius=0)
map_widget.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)


page1 = Frame(window)
page2 = Frame(window)
page3 = Frame(window)
 
for frame in (page1, page2, page3):
    frame.grid(row=0, column=0, sticky="nsew")
You are using 2 geometry managers in the same widget. This produces unpredictable results. Try it with grid, row=1 or column=1, for map_widget.

I don't understand what you want. Do you want page1, 2, or 3 on the screen and map_widget to show no matter which page is showing? You might want to use grid_forget() https://insolor.github.io/effbot-tkinter...-index.htm and/or possibly Toplevel for a 2nd window.
Reply
#3
Your main problem is you put the map in the window instead of page_2. When your program pulls up page_2, it does not draw the map because the map is not on page_2, it is on the window, hidden behind page_1, page_2 and page_3.

There should be one "next" button that is in window, and window is in charge of which page gets displayed. Like this:
import tkinter as tk
from tkintermapview import TkinterMapView


class Page(tk.Frame):
    """A special frame that displays content in MyWindow."""
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        parent.add_page(self)


class MyWindow(tk.Tk):
    """A window that selects one of several pages to display."""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.next_button = tk.Button(
            self, text="Next", font=(None, 16), command=self.next_page
        )
        self.next_button.grid(row=1, column=0, sticky="news", padx=5, pady=5)
        self.pages = []
        self.page = 0

    def add_page(self, page):
        """Add a page to the page list."""
        self.pages.append(page)
        page.grid(row=0, column=0, sticky="news")
        self.set_page(len(self.pages)-1)

    def set_page(self, index):
        """Select which page to display."""
        self.page = index
        if len(self.pages) > 1:
            self.pages[index].tkraise()

    def next_page(self):
        """Select the next page."""
        self.set_page((self.page + 1) % len(self.pages))


window = MyWindow()

page = Page(window, background="blue")
label = tk.Label(page, text="Page 1", bg="blue", font=(None, 32))
label.pack(expand=True, fill=tk.BOTH)

page = Page(window)
map_widget = TkinterMapView(page, width=800, height=600, corner_radius=0)
map_widget.pack(expand=True, fill=tk.BOTH)
map_widget.set_position(60.03345, 11.35806)  # Norway
map_widget.set_address("fjellvegen 4, auli, norway", marker=True)
map_widget.set_zoom(16)

page = Page(window, background="green")
label = tk.Label(page, text="Page 3", bg="green", font=(None, 32))
label.pack(expand=True, fill=tk.BOTH)

window.set_page(0)
window.mainloop()
Next time you have a question, please take some time to prepare your code for posting. I originally skipped this question because I thought it was about kivy, and I am not familiar with kivy.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 1,027 Jan-16-2024, 06:44 PM
Last Post: rob101
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,599 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 4,155 Sep-30-2021, 05:57 PM
Last Post: menator01
  tkinter frame camera opencv Nick_tkinter 9 5,483 Mar-21-2021, 06:41 PM
Last Post: Nick_tkinter
  [Tkinter] Tkinter delete values in Entries, when I'm changing the Frame robertoCarlos 11 5,879 Jul-29-2020, 07:13 PM
Last Post: deanhystad
  How to disable focus on Frame in Tkinter? szafranji 1 3,038 May-13-2020, 10:45 PM
Last Post: DT2000
  [Tkinter] Tkinter bringing variables from 1 frame to another zukochew 6 12,589 Dec-26-2019, 05:27 AM
Last Post: skm
  Unable to put background image on Tkinter Frame jenkins43 2 8,778 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 9,037 Sep-30-2019, 07:30 AM
Last Post: Maksim
  TKinter GUI / Pandas Data frame Loop NSearch 0 6,078 Jun-26-2019, 12:37 AM
Last Post: NSearch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020