Home > Software engineering >  TypeError: get() missing 1 required positional argument: 'self' / Exception in Tkinter cal
TypeError: get() missing 1 required positional argument: 'self' / Exception in Tkinter cal

Time:12-02

So i've been working on this project and for some reason this error keeps poping up:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "D:\KPI\minesweeper\main.py", line 198, in login_verify
    username_1 = username_verify.get()
TypeError: get() missing 1 required positional argument: 'self'

At first I've tried to combine .pack and grid in one class, but error occured, so I just put it off the class. Then I tried to add this .pack piece of code to the new class, but there still was a problem. TBO, this code kinda has different elements from different code, so i might've just got confused.

Here's a code:

from random import shuffle
from tkinter import *
from tkinter.messagebox import showinfo
import os

colors = {
    0: 'white',
    1: 'blue',
    2: 'green',
    3: 'red',
    4: 'purple',
    5: 'maroon',
    6: 'turquoise',
    7: 'black',
    8: 'gray',
}


class Autherisation:
    def __init__(self, master):
        self.screen = screen


def delete2():
    screen3.destroy()


def delete3():
    screen4.destroy()


def delete4():
    screen5.destroy()

def login_sucess():
    global screen3
    screen3 = Toplevel(screen)
    screen3.title('Sucess')
    screen3.geometry('150x100')
    Label(screen3, text='Login sucess').pack()
    Button(screen3, text='OK', command=delete2).pack()


def password_not_recognised():
    global screen4
    screen4 = Toplevel(screen)
    screen4.title('Sucess')
    screen4.geometry('150x100')
    Label(screen4, text='Password not recognised').pack()
    Button(screen4, text='OK', command=delete3).pack()


def user_not_found():
    global screen5
    screen5 = Toplevel(screen)
    screen5.title('Sucess')
    screen5.geometry('150x100')
    Label(screen5, text='User not found').pack()
    Button(screen5, text='OK', command=delete4).pack()


def register_user():
    username_info = username_.get()
    password_info = password_.get()

    file = open(username_info, "w")
    file.write(username_info   '\n')
    file.write(password_info)
    file.close()

    username_entry.delete(0, END)
    password_entry.delete(0, END)

    Label(screen_1, text='Registration successful!', fg='green', font=('calibri', 11)).pack()


def login_verify():
    username_1 = username_verify.get()
    password_1 = password_verify.get()
    username_entry1.delete(0, END)
    password_entry1.delete(0, END)

    list_of_files = os.listdir()
    if username_1 in list_of_files:
        file1 = open(username_1, 'r')
        verify = file1.read().splitlines()
        if password_1 in verify:
            login_sucess()
        else:
            password_not_recognised()
    else:
        user_not_found()


def register():
    global screen_1
    screen_1 = Toplevel(screen)
    screen_1.title("Register")
    screen_1.geometry('300x250')

    global username_
    global password_
    global username_entry
    global password_entry
    username_ = StringVar()
    password_ = StringVar()

    Label(screen_1, text='Please enter details below').pack()
    Label(screen_1, text='').pack()
    Label(screen_1, text='Username * ').pack()
    username_entry = Entry(screen_1, textvariable=username_)
    username_entry.pack()
    Label(screen_1, text='Password * ').pack()
    password_entry = Entry(screen_1, textvariable=password_)
    password_entry.pack()
    Button(screen_1, text='Register', width=10, height=1, command=register_user).pack()


def login():
    global screen_2
    screen_2 = Toplevel(screen)
    screen_2.title("Login")
    screen_2.geometry('300x250')
    Label(screen_2, text='Please enter details below to login').pack()
    Label(screen_2, text='').pack()

    global username_verify
    global password_verify

    username_verify = StringVar
    password_verify = StringVar
    global username_entry1
    global password_entry1
    Label(screen_2, text='Username * ').pack()
    username_entry1 = Entry(screen_2, textvariable=username_verify)
    username_entry1.pack()
    Label(screen_2, text='').pack()
    Label(screen_2, text='Password * ').pack()
    password_entry1 = Entry(screen_2, textvariable=password_verify)
    password_entry1.pack()
    Label(screen_2, text='').pack()
    Button(screen_2, text='Login', width=10, height=1, command=login_verify).pack()


def login_screen():
    global screen
    screen = Tk()
    screen.geometry("300x250")
    screen.title("Minesweeper")
    Label(text='MineSweeper', bg='grey', width='300', height='2', font=("calibri", 13)).pack()
    Label(text='').pack()
    Button(text='Login', height='2', width='30', command=login).pack()
    Label(text='').pack()
    Button(text="Register", height='2', width='30', command=register).pack()

    screen.mainloop()


login_screen()


class MyButton(tk.Button):

    def __init__(self, master, x, y, number=0, *args, **kwargs):
        super(MyButton, self).__init__(master, width=3, font='Calibri 15 bold', *args, **kwargs)
        self.x = x
        self.y = y
        self.number = number
        self.is_mine = False
        self.count_bomb = 0
        self.is_open = False

    def __repr__(self):
        return f'MyButton{self.x} {self.y} {self.number} {self.is_mine}'


class MineSweeper:
    window = tk.Tk()
    window.title("MineSweeper")
    row_ = 16
    columns_ = 16
    mines = 60
    IS_GAME_OVER = False
    IS_FIRST_CLICK = True

    def __init__(self):
        self.buttons = []

        for i in range(MineSweeper.row_   2):
            temp = []
            for j in range(MineSweeper.columns_   2):
                btn = MyButton(MineSweeper.window, x=i, y=j)
                btn.config(command=lambda button=btn: self.click(button))
                btn.bind('<Button-3>', self.right_click)
                btn.bind('<Double-Button-1>', self.double_button_click)
                temp.append(btn)
            self.buttons.append(temp)

    def right_click(self, event):
        if MineSweeper.IS_GAME_OVER:
            return
        cur_btn = event.widget
        if cur_btn['state'] == 'normal':
            cur_btn['state'] = 'disabled'
            cur_btn['text'] = '           
  • Related