![]() |
tkinter mapview in a frame - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: tkinter mapview in a frame (/thread-40213.html) |
tkinter mapview in a frame - janeik - Jun-21-2023 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() RE: tkinter mapview in a frame - woooee - Jun-22-2023 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-tkinterbook-archive/tkinter-index.htm and/or possibly Toplevel for a 2nd window. RE: tkinter mapview in a frame - deanhystad - Jun-22-2023 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. |