Home > front end >  I want to make button commands in one class in python tkinter
I want to make button commands in one class in python tkinter

Time:02-03

I am getting the following error:
log_print() missing 1 required positional argument: 'self' I would really appreciate it if you could tell me how to fix it. ''' import tkinter as tk

class SimpleApp(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.btn1 = tk.Button(self.parent, text="start", width=10, height=5, command = Button1.log_print)
        self.btn2 = tk.Button(self.parent, text="stop", width=10, height=5)                  
        self.textbox = tk.Text(self.parent, height = 10)
        self.btn1.grid(row = 0, column = 0,sticky = "news", padx= 5)
        self.btn2.grid(row = 0, column = 1, sticky = "news", padx= 5)
        self.textbox.grid(row = 1, column = 0, columnspan = 2, sticky = "news", padx= 5)
    

class Button1(SimpleApp):
    def __init__(self, parent, textbox):
        SimpleApp.__init__(self, parent, textbox)
    
    def log_print(self):
        self.textbox.insert("end", "1")
        self.textbox.update()
        self.textbox.see("end")


        
if __name__ == "__main__":
    root = tk.Tk()
    SimpleApp(root).grid()
    root.mainloop()

'''

CodePudding user response:

You mean something like this ?

import tkinter as tk


class SimpleApp(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.textbox = tk.Text(self.parent, height=10)
        self.btn1 = tk.Button(self.parent, text="start", width=10, height=5, command=Button1(self).log_print)
        self.btn2 = tk.Button(self.parent, text="stop", width=10, height=5)
        self.btn1.grid(row=0, column=0, sticky="news", padx=5)
        self.btn2.grid(row=0, column=1, sticky="news", padx=5)
        self.textbox.grid(row=1, column=0, columnspan=2, sticky="news", padx=5)


class Button1:
    def __init__(self, simple_app_object):
        self.simple_app = simple_app_object

    def log_print(self):
        self.simple_app.textbox.insert("end", "1")
        self.simple_app.textbox.update()
        self.simple_app.textbox.see("end")


if __name__ == "__main__":
    root = tk.Tk()
    SimpleApp(root).grid()
    root.mainloop()
  •  Tags:  
  • Related