Python Forum
[Tkinter] should not be getting this exception...confused - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] should not be getting this exception...confused (/thread-33811.html)



should not be getting this exception...confused - MrP - May-29-2021

Hello,

I am trying to learn python class programming and I have run into an exception when trying to run a small program I found on the internet. It has me confused as I see people using the line of code with no problem

Error:
builtins.TypeError: Buttons() takes no arguments
the snippit of code I am referring to is

if __name__ == '__main__':
    root = tk.Tk()
    b = Buttons(root)
    root.mainloop()
can anybody explain why I getting this exception?


RE: should not be getting this exception...confused - snippsat - May-29-2021

Post whole code as import is missing.
So to fix it i guess Buttons should be Button.
import tkinter as tk
from tkinter import Button

if __name__ == '__main__':
    root = tk.Tk()
    b = Button(root)
    root.mainloop()
Quote:I am trying to learn python class programming
Code over is not class programing,look at A Simple Hello World Program
So here use a class which is always advisable to use when doing GUI programming.


RE: should not be getting this exception...confused - MrP - May-29-2021

This is the whole code
import tkinter as tk


class Buttons:
    def __init(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.b1 = tk.Button(self.master, text="Button1", command=self.display)
        self.b2 = tk.Button(self.master, text="Button2", command=self.new_window)
        self.b1.pack()
        self.b2.pack()
        self.frame.pack()
        
    def display(self):
        print("Hello Button1")
        
    def new_window(self):
        self.master.withdraw()
        self.newWindow = tk.Toplevel(self.master)
        
        
class Buttons1():
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.frame.pack()
        
        
if __name__ == '__main__':
    root = tk.Tk()
    b = Buttons(root)
    root.mainloop()



RE: should not be getting this exception...confused - snippsat - May-29-2021

Missing __init __ at line 5.
 def __init__(self, master):
Buttons is a little confusing name as is close to Button that tkinter use.


RE: should not be getting this exception...confused - Yoriz - May-29-2021

I think inheriting from tkinter widgets is better.
import tkinter as tk


class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.buttons_frame = ButtonsFrame(self)


class ButtonsFrame(tk.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.btn1 = tk.Button(self, text="Button1", command=self.on_btn1)
        self.btn_2 = tk.Button(self, text="Button2", command=self.on_btn2)
        self.btn1.pack()
        self.btn_2.pack()
        self.pack()

    def on_btn1(self):
        print("Hello Button1")

    def on_btn2(self):
        self.master.withdraw()
        self.on_btn2 = tk.Toplevel(self.master)


if __name__ == '__main__':
    app = App()
    app.mainloop()
See the following post for an example of tkinter using classes.
https://python-forum.io/thread-33788-post-142731.html#pid142731