Home > Back-end >  Pass Data between Windows
Pass Data between Windows

Time:12-19

I am new in this forum and have a question that deals with me for days. For exercise purposes, I created two Windows, the second Windows is called as module "textbetrachter" from first Window (MainWindow). As far as all, the second window is called via a key combination "Self.Bind (" ", Self.win2)" and that works as well as I said.

My question is: How can I pass the variable "liste" (values) on the 2nd Window (Toplevel), to continue working there. Exactly at this point I just make a programming error. The error message is:

Print (self.win.list) AttributeError: 'Toplevel' Object Has No Attributes 'List'

Here's the program I made by me:

import tkinter as tk
from tkinter import ttk
import textbetrachter

class MainWindow(tk.Tk):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.title("Lektion: Frame-Widget")

        frame_01 = tk.Frame(self, bg="red", width=600, height=200)
        frame_01.grid(column=0, row=0, padx=5, pady=5, sticky="w")

        frame_01a = tk.LabelFrame(frame_01, text="Rahmen 1:", bg="gray", width=330, height=190)
        frame_01a.grid(column=0, row=0, padx=5, pady=5)

        frame_01b = tk.LabelFrame(frame_01, text="Rahmen 2:", bg="lightgray", width=330, height=190)
        frame_01b.grid(column=2, row=0, padx=(0,5), pady=5)

        self.liste  = frame_01.config()

        self.bind("<Control-s>",self.win2)

    def win2(self, event):
        self.mein_textbetrachter = textbetrachter.Textbetrachter()
        #self.mein_textbetrachter.win.liste
        self.mein_textbetrachter.win.mainloop()


root = MainWindow()
root.mainloop()

And this is the module "textbetrachter" (saved as textbetrachter.py):

import tkinter as tk
from tkinter import ttk

class Textbetrachter():
    def __init__(self):
        self.win = tk.Toplevel()
        self.win.title("Textbetrachter")
        self.win.resizable(False, False)

        frame_02 = tk.Frame(self.win, bg="green")
        frame_02.grid(column=0, row=0, padx=5, pady=5)

        texteditor = tk.Text(frame_02, bg="lightyellow", height=20)
        texteditor.grid(column=0, row=0, padx=5, pady=5)

        text_scroller = tk.Scrollbar(frame_02, orient="vertical", command=texteditor.yview)
        text_scroller.grid(column=1, row=0, sticky="ns")


        print(self.win.liste)

        for item in self.win.liste.config():
               texteditor.insert(tk.INSERT, (item, ":", self.win.liste[item], "\n"))

So here in the second window. I would like to continue working with the variables "liste", but unfortunately that does not work. I would be very happy if someone could help me here.

Greetings SC19

CodePudding user response:

You can pass the list to the second window.

class Textbetrachter():
    def __init__(self, liste):
        self.liste = liste
        ...

class MainWindow(tk.Tk):
    ...
    def win2(self, event):
        self.mein_textbetrachter = textbetrachter.Textbetrachter(self.liste)
        ...

If there's more than one variable you need to share, then pass the instance of MainWindow instead:

class Textbetrachter():
    def __init__(self, mainwindow):
        self.mainwindow = mainwindow
        ...
        print(self.mainwindow.liste)

class MainWindow(tk.Tk):
    ...
    def win2(self, event):
        self.mein_textbetrachter = textbetrachter.Textbetrachter(self)
        ...

Also, you shouldn't call mainloop in MainWindow. Your program only needs a single event loop.

  • Related