Python Forum
Problem passing parameters between windows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem passing parameters between windows
#1
Hello all,
When my program is run, it prompts for a table name. After entering a table name and clicking on the "Delete Fields" button, the delete_fields subroutine gets called. The delete_fields subroutine calls the get_password subroutine which opens a new window and prompts for a password. After entering the password and clicking on the "Confirm Delete" button, the confirm_delete subroutine checks if the password is correct.

Depending if the password matches, I need the confirm_delete subroutine to return True or False back to the delete_fields subroutine to determine whether or not to delete the fields. Unfortunately, the code is not working.
If somebody can figure out what is wrong with the code, it would be greatly appreciated.
Thanks in advance.



from tkinter import *
from tkinter import ttk
import tkinter.font as font
from config import hostname,username,passwd

def confirm_delete(new_window,pwd):
    if (pwd.get() == passwd):
        new_window.destroy()
        return(True)
    else:
        new_window.destroy()
        return(False)

def get_password():
	new_window = Toplevel(mw)
	new_window.wm_title("Enter Credentials")
	new_window.geometry('700x200+400+200')

	new_frame2 = Frame(new_window)
	new_frame3 = Frame(new_window)
	new_frame2.pack(side=TOP,fill=X)
	new_frame3.pack(side=BOTTOM,fill=X)

	label1 = Label(new_frame2, text="Password",font=("Times",16))
	label1.grid(row=1,column=0)
	pwd = Entry(new_frame2,width=40,font=("Times",16),show="*")
	pwd.grid(row=1,column=1)

	btn = Button(new_frame3,text='Confirm Delete',font=("Times",16),command= lambda:confirm_delete(new_window,pwd)).pack()
	new_window.mainloop()


def delete_fields():
    print("Entering delete_fields subroutine")
    if get_password():
        print("Deleting fields")
    print("Leaving delete_fields subroutine")


# Start the main program here
if __name__ == "__main__":
    current_file = __file__
    mw=Tk()
    mw.geometry('700x200+400+200')
    mw.title(current_file)

    frame2 = Frame(mw)
    framebot = Frame(mw)
    frame2.pack(side=TOP,fill=X)
    framebot.pack(side=BOTTOM,fill=X)

    w2 = Label(frame2, text="Table Name: ",font=("Times",16)).pack(side="left")
    a2 = ttk.Combobox(frame2,width=40,font=("Times",16))
    a2.pack(side="left")

    btn2 = Button(framebot,text='Delete Fields',font=("Times",16),command=delete_fields).pack(side="left")
    btn6 = Button(framebot,text='Exit',font=("Times",16),command=mw.quit).pack(side="right")

    mw.mainloop()
Reply
#2
The confirm_delete function should delete the field, either by adding the code to the function, or having it call the function that deletes the field. Your problem is not one of passing information (could be done with a global variable), but knowing when to act on information. After drawing the "Enter Credentials" window your code does not stand around waiting for the "Confirm Delete" button to be pressed. get_password() is going to return immediately. Even if get_password() returned a value, the value would be wrong because the user has not yet entered a password or pressed the confirm button. Only after pressing the button does the program know if the field can be deleted. That means your code that deletes a field must be called after the button is pressed.
Reply
#3
Thanks deanhystad.

I had already written the delete_fields and delete_table subroutines that get called when a button is clicked on the main screen. I just wanted to add a password confirmation screen (similar to a messagebox) before actually calling the delete routines. Apparently this cannot be done.

I reorganized the code so that when you click on the "Delete Fields" or "Delete Tables" button, it calls the password confirmation routine. Then if the password is valid, it calls the appropriate delete routine.
Below is the code.

from tkinter import *
from tkinter import ttk
import tkinter.font as font
from config import hostname,username,passwd

def confirm_delete(table_or_fields):
	new_window = Toplevel(mw)
	new_window.wm_title("Enter Credentials")
	new_window.geometry('700x200+400+200')

	new_frame2 = Frame(new_window)
	new_frame3 = Frame(new_window)
	new_frame2.pack(side=TOP,fill=X)
	new_frame3.pack(side=BOTTOM,fill=X)

	label1 = Label(new_frame2, text="Password",font=("Times",16))
	label1.grid(row=1,column=0)
	pwd = Entry(new_frame2,width=40,font=("Times",16),show="*")
	pwd.grid(row=1,column=1)

	btn = Button(new_frame3,text='Confirm Delete',font=("Times",16),command= lambda:call_delete_rtn(new_window,pwd,table_or_fields)).pack()
#	new_window.mainloop()


def call_delete_rtn(new_window,pwd,table_or_fields):
    if (pwd.get() == passwd):
        if (table_or_fields == "table"):
            delete_table()
        else: #(table_or_fields == "fields")
            delete_fields()
        new_window.destroy()
    else:
        new_window.destroy()


def delete_fields():
    print("Entering delete_fields subroutine")
    # code to delete fields

def delete_table():
    print("Entering delete_table subroutine")
    # code to delete table


# Start the main program here
if __name__ == "__main__":
    current_file = __file__
    mw=Tk()
    mw.geometry('700x200+400+200')
    mw.title(current_file)

    frame2 = Frame(mw)
    framebot = Frame(mw)
    frame2.pack(side=TOP,fill=X)
    framebot.pack(side=BOTTOM,fill=X)

    w2 = Label(frame2, text="Table Name: ",font=("Times",16)).pack(side="left")
    a2 = ttk.Combobox(frame2,width=40,font=("Times",16))
    a2.pack(side="left")

    btn2 = Button(framebot,text='Delete Fields',font=("Times",16),command=lambda:confirm_delete("field")).pack(side="left")
    btn3 = Button(framebot,text='Delete Table',font=("Times",16),command=lambda:confirm_delete("table")).pack(side="left")
    btn6 = Button(framebot,text='Exit',font=("Times",16),command=mw.quit).pack(side="right")

    mw.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem passing argument to functionin inTkinter frame ericwm7248 3 1,000 Sep-11-2023, 03:11 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020