Home > Blockchain >  python Tkinter call function between class gets TypeError __init__() missing 1 required positional a
python Tkinter call function between class gets TypeError __init__() missing 1 required positional a

Time:11-19

Error message occur when function "chk_file_exist" in class "Rightframe" calls antoher function

Leftframe().update_listbox()###error TypeError: init() missing 1 require d positional argument: 'parent' TypeError: init() missing 1 required positional argument: 'parent'

GUI Image: Leftframe has a listbox, Rightframe has two buttons(start and stop)

code floor:

1.function empty_listbox in class Leftframe generates empty listbox

2.User press start_btn in Rightframe (another script start generate a file 'XXX.pkl')

3.function chk_file_exist in class Rightframe will waits until 'XXX.pkl' exists(I have force this condition be True for debugging)

4.function chk_file_exist in class Rightframe calls function update_listbox in class Leftframe to parse 'XXX.pkl', ... (error happened)

my code:

import tkinter as tk
import os

class Leftframe(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.scrollbar = tk.Scrollbar(self)
        self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
        self.mylist = tk.Listbox(self, yscrollcommand=self.scrollbar.set)
        self.mylist.place(x=0, y=0, width=260, height=410)
        self.scrollbar.config(command=self.mylist.yview)
        self.empty_listbox()
    def empty_listbox(self):
        self.mylist.delete(0, tk.END)
        for it in ([' ']):
            self.mylist.insert(tk.END, it)
    def update_listbox(self):
        self.mylist.delete(0, tk.END)
        #do something ... parsed a file, updates listbox
        #for it in parsedlist:
        #    self.mylist.insert(tk.END, it)

class Rightframe(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.start_btn = tk.Button(self,text='Start')
        self.start_btn.config(command=self.button_start_push)
        self.start_btn.place(anchor=tk.NW,x=10,y=10,width=60,height=20)
        self.stop_btn = tk.Button(self,text='Stop')
        self.stop_btn['state'] = tk.DISABLED
        self.stop_btn.config(command=self.button_stop_push)
        self.stop_btn.place(anchor=tk.NW,x=10,y=60,width=60,height=20)

    def chk_file_exist(self):
        if 1:#os.path.isfile(os.path.join(os.curdir, fileroute)): force for debugging!!
            Leftframe().update_listbox()###error TypeError: __init__() missing 1 required positional argument: 'parent'
        else:
            print('file generating, please wait')#other script generating file in background
            main_win.after(1000, self.chk_file_exist)
    def button_start_push(self):
        print('Started')
        self.start_btn['state'] = tk.DISABLED
        self.stop_btn['state'] = tk.NORMAL
        global fileroute
        self.chk_file_exist()

    def button_stop_push(self):
        print('stop button pressed')
        self.start_btn['state'] = tk.NORMAL
        open(os.path.join(os.curdir, 'stop.txt'), 'a').close()


if __name__ == '__main__':
    fileroute = 'XXX.pkl'
    main_win = tk.Tk()
    main_win.geometry("200x150 0 0")
    Leftframe(main_win, relief="sunken", borderwidth=1).place(x=10, y=10, width=80, height=100)
    Rightframe(main_win, relief="sunken", borderwidth=1).place(x=100, y=10, width=80, height=100)
    main_win.mainloop()

I'm trying to make my code object oriented like this link below Best way to structure a tkinter application? Should I use window as class rather than frame? or should I just put all elements(listbox, button, entry, ...)into one window class,not using frame?

I am appreciate all your suggestion
Many thanks!!

CodePudding user response:

You didn't add the parent widget parameter for the line Leftframe().update_listbox() in function chk_file_exist(self):, so add self.parent(which is main_win) -

Leftframe(self.parent).update_listbox()
  • Related