I have a frame with a check button located in a main window. I need to create a pop up when the check button is clicked, but I face the following error:
open_popup() takes 0 positional arguments but 1 was given
How can I fix this error?
import pandas as pd
import numpy as np
from standard_precip import spi
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter.messagebox import showinfo
import os
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2Tk)
from tkinter import *
from PIL import Image, ImageTk
class EGEDT(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
# dimensions of the main window
master.geometry("300x300")
def create_widgets(self):
self.top_frame = ttk.Frame(self)
padding_x = 8
padding_y = 7
self.working_dir = "/"
# Create a check button
self.ab_frame = ttk.Frame(self.top_frame)
self.ab_label = ttk.Label(self.ab_frame, text="Select the check button:")
self.ab_label.grid(row=1, column=0, padx= 5, pady= 5)
self.a = ttk.Checkbutton(self.ab_frame,text="SPI", onvalue=1, offvalue=0,command=self.open_popup)
self.a.grid(row=2, column=1, padx= 0, pady= 0)
self.ab_frame.grid(row=2, column=0, sticky=tk.W, padx=padding_x, pady=padding_y)
self.top_frame.grid(row=0, column=0)
def open_popup():
top= Toplevel(self.top_frame)
top.geometry("750x250")
top.title("Child Window")
Label(top, text= "Hello World!", font=('Mistral 18 bold')).place(x=150,y=80)
def main():
root = tk.Tk()
root.title("Main window")
app = EGEDT(master=root)
app.mainloop()
main()
>Solution :
You need to include the self
parameter in the definition of open_popup
since it’s a method of the EGEDT
class, and the class is passed to the method as an implicit first argument
def open_popup(self): # add 'self' here
top = Toplevel(self.top_frame)
top.geometry("750x250")
top.title("Child Window")
Label(top, text= "Hello World!", font=('Mistral 18 bold')).place(x=150,y=80)