0

I'm getting the error TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given but i have no idea why??

I have one window which then calls a function to open a second window. This second window uses classes and throws up the error. Here are the lines of code that cause the error:

    def openadmin():  
        class SCapp(tk.Toplevel(window)):
            def __init__(self, *args, **kwargs):
                tk.Toplevel.__init__(self, *args, **kwargs)

(Obviously that's not all the code, just the part which throws up the TypeError)

("window" is the first window which calls the function openadmin() to open the second window)

Any help would be appreciated because this is my coursework!

Edit:

IGNORE PREV CODE ^^

Here is a basic version of working code for the second window which uses frames (I applied some changes already):

import tkinter as tk

class SCapp(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 (StartPage,): #list of frames
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame("StartPage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label1 = tk.Label(self,text='This is the start page of 2nd window')
        label1.pack()

def openadmin():
    if __name__ == "__main__":
        app = SCapp()
        app.title("SC App")
        app.attributes("-topmost", True)
        app.mainloop()

And here is the first window (in a separate file) which is supposed to open the previous window:

import tkinter as tk
from testcode import *

def onClick():
    if False:
        return #some code is here
    else:
        openadmin()


def WindowOne():
    global entid, entpassword 
    window = tk.Tk()
    window.wm_title("Window 1")
    label1 = tk.Label(text='This is the first window')
    label1.pack()
    enter = tk.Button(window, text = 'Click', command = onClick)
    enter.pack()

    window.mainloop()

WindowOne()

When these are in the same file it works fine, but when they are in separate files (like i need them to be) nothing happens when the button is clicked.

Suzie M
  • 1
  • 2
  • ***"just the part which throws up the TypeError)"***: This sould read: `class SCapp(tk.Toplevel):` and `window` goes to `parent`, e.g. `__init__(self, parent, *args, **kwargs):` and `parent` goes to `.Toplevel.__init__(self, parent, ... ` – stovfl Nov 16 '19 at 21:09
  • Sorry, still doesn't seem to be working. If change the line to `class SCapp(tk.Toplevel):` then the function doesn't work at all and the window doesn't even show up – Suzie M Nov 16 '19 at 21:28
  • ***"nothing happens"***: This: `if __name__ == "__main__":` is wrong usage, read [executing-modules-as-scripts](https://docs.python.org/3/tutorial/modules.html#executing-modules-as-scripts). Furthermore wrong, read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) and [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) – stovfl Nov 18 '19 at 21:11

0 Answers0