Python Forum
pass a variable between tkinter and toplevel windows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pass a variable between tkinter and toplevel windows
#9
(Oct-03-2023, 01:48 PM)deanhystad Wrote: I would do it like this:
import tkinter as tk
import customtkinter as ctk


class OverlayWindow(tk.Toplevel):
    def __init__(self, overlay_file):
        self.overlay_file = overlay_file
        super().__init__()
        self.geometry("1000x1000")   
        self.resizable(False, False) 
        self.overrideredirect(True)
        tk.Button(self, text="Submit", command=self.loading).place(x=45,y=1140)

    def loadimg(self):
        """Set overlay image"""
        photo = tk.PhotoImage(file=self.overlay_file.get().strip())
        image_label = tk.Label(self, image=photo)
        image_label.photo = photo
        image_label.place(x=0, y=0)


class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("1920x1200+0+0")
        self.minsize(1920, 1200)
        self.resizable(False, False)
        self.overrideredirect(True)
        self.attributes('-topmost',True)

        self.overlay_file = tk.StringVar()
        ctk.CTkEntry(
            self,
            textvariable=self.overlay_file,
            width=360,
            font=("Ariel", 14, "bold"),
            placeholder_text="Sti til bilde 1 som skal behandles.(d:/bilde/abc.png)",
            justify=("left")).place(x=12,y=1010)

window = MainWindow()
top = OverlayWindow(window.overlay_file)
If there are several values that top needs to get from window, I pass window to top, and use that to reference the values.
class OverlayWindow(tk.Toplevel):
    def __init__(self, main_window):
        self.main_window = main_window
        super().__init__()
        self.geometry("1000x1000")   
        self.resizable(False, False) 
        self.overrideredirect(True)
        tk.Button(self, text="Submit", command=self.loading).place(x=45,y=1140)

    def loadimg(self):
        """Set overlay image"""
        photo = tk.PhotoImage(file=self.main_window.overlay.get().strip())
        image_label = tk.Label(self, image=photo)
        image_label.photo = photo
        image_label.place(x=0, y=0)
Another approach is to make a data model that is used by both windows. Think of it as a blackboard that both windows can read and write.
import tkinter as tk
import customtkinter as ctk


class ApplicationData():
    """I have all data shared between the main and overlay window."""
    def __init__(self):
        self.overlay_file = tk.StringVar()
        # additional shared vaiables go here


class OverlayWindow(tk.Toplevel):
    def __init__(self, data):
        self.data = data
        super().__init__()
        self.geometry("1000x1000")   
        self.resizable(False, False) 
        self.overrideredirect(True)
        tk.Button(self, text="Submit", command=self.loading).place(x=45,y=1140)

    def loadimg(self):
        """Set overlay image"""
        photo = tk.PhotoImage(file=self.data.overlay_file.get().strip())
        image_label = tk.Label(self, image=photo)
        image_label.photo = photo
        image_label.place(x=0, y=0)


class MainWindow(tk.Tk):
    def __init__(self, data):
        super().__init__()
        self.geometry("1920x1200+0+0")
        self.minsize(1920, 1200)
        self.resizable(False, False)
        self.overrideredirect(True)
        self.attributes('-topmost',True)

        ctk.CTkEntry(
            self,
            textvariable=self.data.overlay_file,
            width=360,
            font=("Ariel", 14, "bold"),
            placeholder_text="Sti til bilde 1 som skal behandles.(d:/bilde/abc.png)",
            justify=("left")).place(x=12,y=1010)


data = ApplicationData()
window = MainWindow(data)
top = OverlayWindow(data)
You should start studying Python classes. There is a reason that tkinter and customtkinter use classes. It is hard to write good looking GUI code without them.
Thanks for thee advices. I will probably change to oop affter Im finished without oop :)
Reply


Messages In This Thread
RE: pass a variable between tkinter and toplevel windows - by janeik - Oct-05-2023, 03:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter two windows instead of one jacksfrustration 7 1,018 Feb-08-2024, 06:18 PM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 947 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 2,033 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Toplevel window menator01 5 3,180 Apr-18-2022, 06:01 PM
Last Post: menator01
  how to add two numbers and pass the result to the next page in tkinter? pymn 7 4,465 Feb-15-2022, 04:40 AM
Last Post: pymn
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 4,037 Jan-07-2022, 10:10 PM
Last Post: finndude
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 5,012 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,364 Oct-15-2021, 08:01 AM
Last Post: drSlump
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,738 Mar-10-2021, 04:21 PM
Last Post: Sir
  [Tkinter] Images in Toplevel() finndude 4 4,409 Mar-09-2021, 09:39 AM
Last Post: finndude

Forum Jump:

User Panel Messages

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