Home > database >  python: defining a base tkinter class for user input but adding labels in a sub class: missing posit
python: defining a base tkinter class for user input but adding labels in a sub class: missing posit

Time:03-02

So I have this code divided between two different modules. First one, called 'GUIs' where I want to store all the TKINTER codes and then another from where I call the GUI.

Since I am using that base for other modules I was thinking about adding a subclass that would add 2 labels with text for the user to read and not disturb the base GUI for the other modules. The thing is that it is not working.

It tells me that 'add_labels()' is missing 1 required positional argument: 'self'.

Would appreciate some help. I am copying below the 2 codes:

# GUIs module
import tkinter as tk

class pbt_GUI(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Manual Input")
        self.resizable(0,0)
        # create widgets
        self.frame = tk.Frame(self, width=300, height=300)
        self.GUI_date = tk.Label(self.frame, text='Date (mm/dd/yyyy):').grid(row=0, sticky='w')
        self.GUI_date_input = tk.Entry(self.frame)
        self.submit = tk.Button(self.frame, text="Submit")
        # widgets layout
        self.frame.grid()
        self.GUI_date_input.grid(row=0, column=1)
        self.submit.grid(row=5)

        self.username = tk.Label(self.frame, text='username: bla')
        self.password = tk.Label(self.frame, text='password: bla**')

    def add_labels(self):
        self.username.grid(row=3)
        self.password.grid(row=4)

And then there is the other module:

# module where executed
from datetime import datetime
import generalUse.GUIs

date_input = ('')

def get_man_input():
    global date_input

    date_input = datetime.strptime(UI_GUI.GUI_date_input.get(), '%m/%d/%Y')
    date_input.strftime('%Y/%m/%d').date
    UI_GUI.destroy()


# Button set up for executing the GUI:
UI_GUI = generalUse.GUIs.pbt_GUI.add_labels()
UI_GUI.submit['command'] = get_man_input
UI_GUI.mainloop()

Thank you very much in advance

CodePudding user response:

You need to create an instance of pbt_GUI and use this instance to call add_labels():

...
UI_GUI = generalUse.GUIs.pbt_GUI() # create instance of pbt_GUI
UI_GUI.add_labels()
...
  • Related