简体   繁体   中英

I can't seem to pass on an object to another class in Python/Tkinter

I have an object which I want to pass to a new frame through a method in another frame class. I found a solution that looked similar to what I was after and tried it, however, the object doesn't get passed. In fact, I got an error message saying "Value after * must be an iterable, not PackingList", where PackingList is the class which the object is made out of. I tried to instead get the "name" attribute of the object and pass it to the next frame, but it just returns an empty tuple. I am really stuck and I appreciate some help.

class App(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side = "top", fill = "both", expand = True)
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        self.frames = {}

        for F in (Homescreen, Menuscreen, Create, ShowUpcoming, ShowAll, OpenList, Search, Edit):

            frame = F(container, self, *args, **kwargs)
            self.frames[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(Homescreen)

    def show_frame(self, container, *args, **kwargs):

        frame = self.frames[container]
        frame.tkraise()

...

class ShowAll(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        tk.Label(self, text = "Alla packningslistor", font = ("Times new roman", 30)).place(x = 110, y = 0)
        self.controller = controller
        objectList = PK.main()
        objectOpen = PK.showAll(objectList)

        self.radiobutton_list = []
        self.objectList = objectList

        for i in range(len(objectOpen)):
            radiobutton = tk.Radiobutton(self, text = objectOpen[i], command = functools.partial(self.objectToOpen, idx = i)).place(x = 150, y = 100 + i*30)
            self.radiobutton_list.append(radiobutton)
    
    def objectToOpen(self, idx):
        
        objectID = self.objectList[idx]
        
        
        return self.controller.show_frame(OpenList, *objectID)
class OpenList(tk.Frame):

    def __init__(self, parent, controller, *objectID):
        
        tk.Frame.__init__(self, parent)
        #lala = getattr(objectID, "name")
        #print(lala)

As I said I tried to pass just the name of the object but it prints out as an empty tuple in the next frame class.

I did not understand your question. But here are my corrections to your App class. I hope these corrections can help your understanding of Python and tkinter and debug the rest of your codes.

If you need more detailed help, it will be helpful if you can more specific by stating in the comment section what you want to do with classes App , ShowAll , and OpenList (or their method), and I will see how I can help you by elaborating my answer further.

import tkinter as tk


class App(tk.Tk):

def __init__(self, *args, **kwargs):
    super().__init__()
    self.container = tk.Frame(self)
    self.container.pack(side="top", fill="both", expand=True)

    self.frames = {}
    colors = ('white', 'black', 'red', 'green', 'blue', 'cyan', 'yellow', 'magenta')
    frames = ("Homescreen", "Menuscreen", "Create", "ShowUpcoming", "ShowAll", "OpenList", "Search", "Edit")
    col=0
    for C, F in zip(colors, frames):
        print(f'{F=}')
        self.frames[F] = tk.Frame(self.container, background=C, width=100, height=50)
        self.frames[F].grid(row=0, column=col, sticky="nsew")
        # col += 1   # uncomment this to see all frames
    self.show_frame('Create')  # you should see red

def show_frame(self, container):
    self.frames[container].tkraise()


if __name__ == "__main__":
    app = App()
    app.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM