KuBu250
KuBu250

Reputation: 1

Tkinter Button class creation

I am trying to create a class, which will help me to optimize/organize creation of another Tkinter buttons. I am aware that an inheritance should have been used. I've been trying to do something like this:

from tkinter import *    

window = Tk()
window.title("Button class app")

upper_frame = Frame(window)
upper_frame.pack(fill="both", side=TOP, expand=1)

lower_frame = Frame(window)
lower_frame.pack(fill="both", side=BOTTOM, expand=1)

function1 = None
function2 = None

class Buttons(Button):
    def __init__(self, master, text, command):
        Button().__init__(self)  # or super(). method 
        self.master=master
        self.text=text
        self.command=command
        self.font=("Comic Sans", 30)
        self.fg="#7df9ff",
        self.bg="#FFFF00",
        self.activeforeground="#7df9ff",
        self.activebackground="#FFFF00",
        self.state=ACTIVE,
        self.compound='bottom',
button1 = Buttons(upper_frame, "Some text 1", function1)     
button1.pack(fill="x")

button2 = Buttons(lower_frame, "Some text 2", function2)     
button2.pack(fill="x")

I've been trying to use super() method instead of Button().__ init __(), but I am not sure what arguments should I pass on to/what should I really inherit. Using super().

method gives me error: 'Buttons' object has no attribute 'tk'.

Somehow, also with Button(). __ init __ I could also get access to i.e.: self["text"]=text, but not with self.text=text as usual (why?)

I've been also trying to make a class for main Tkinter window, and then for frames - to make inheritance for a Tkinter Button Class object - but I think this is not the way it should work - I think I should make an inheritance from predefined Tkinter Button/tk.Button class.

I will be very grateful for any help or explanations.

Upvotes: 0

Views: 1586

Answers (1)

furas
furas

Reputation: 142631

In other GUIs you could use self.text but tkinter uses

self['text'] = text
self.config(text=text)

And the same with other values (maybe except master)

It allow also

self.config(text=text, command=command)
self.config({"text":text, "command":command})
setting = {'text': 'other', 'bg': 'red'}
self.config(setting)
setting = {'text': 'other', 'bg': 'red'}
self.config(**setting)  # with `**` to unpack dictionary

Full working code.

import * is not preferred - using import tkinter as tk I can create class with name Button (without s) and use three different classes at the same time: tk.Button, ttk.Button and my Button.

In __init__ I use **kwargs so I can send other parameters to class.

import tkinter as tk   # PEP8: `import *` is not preferred

# --- classes ---  # PEP8: all classes after imports

class Button(tk.Button):
    
    def __init__(self, master, text, command, **kwargs):
        super().__init__(master, text=text, command=command)
        
        #self.master = master
        
        #self['text'] = text         # PEP8: spaces around `=`
        #self['command'] = command
        
        self['font'] = ("Comic Sans", 30)
        self['fg'] = "#7df9ff"
        self['bg'] = "#FFFF00"
        self['activeforeground'] = "#7df9ff"
        self['activebackground'] = "#FFFF00"
        self['state'] = 'active'
        self['compound'] = 'bottom'
        
        self.config(**kwargs)

# --- functions ---   # PEP8: all functions after classes (before main code)

def function1():
    print('function1')
    
def function2():
    print('function2')

# --- main ---

window = tk.Tk()
window.title("Button class app")

upper_frame = tk.Frame(window)
upper_frame.pack(fill="both", side=TOP, expand=1)

lower_frame = tk.Frame(window)
lower_frame.pack(fill="both", side=BOTTOM, expand=1)


button1 = Button(upper_frame, "Some text 1", function1, bg='red', activebackground='blue')     
button1.pack(fill="x")

button2 = Button(lower_frame, "Some text 2", function2, bg='green', activebackground='blue')     
button2.pack(fill="x")

window.mainloop()

PEP 8 -- Style Guide for Python Code

Upvotes: 1

Related Questions