Python Forum
[Tkinter] should not be getting this exception...confused
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] should not be getting this exception...confused
#1
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?
Reply
#2
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.
Reply
#3
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()
Reply
#4
Missing __init __ at line 5.
 def __init__(self, master):
Buttons is a little confusing name as is close to Button that tkinter use.
Reply
#5
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-pos...#pid142731
MrP likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020