Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create pop up within a frame in tkinter

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading