Python Forum
[Tkinter] What causes this pop-up to trigger?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] What causes this pop-up to trigger?
#1
Script,
from tkinter import*
import random
import time

local_time = time.asctime(time.localtime(time.time()))#Get local time (TIME)

root = Tk()
root.geometry("1600x800+0+0")
root.title("Book orders !")

text_Input = StringVar()
operator = ""

tops = Frame(root, width = 1600, height = 50, bg = "powder blue", relief = SUNKEN)
tops.pack(side = TOP)

f1 = Frame(root, width = 800, height = 700, bg = "powder blue", relief = SUNKEN)
f1.pack(side = LEFT)

f2 = Frame(root, width = 800, height = 700, bg = "powder blue", relief = SUNKEN)
f2.pack(side = RIGHT)

lbl_info = Label(tops, font = ("arial", 45, "bold"), text = "Book order management system", fg = "Steel Blue", bd = 10, anchor = "w")
lbl_info.grid(row = 0, column = 0)
lbl_info = Label(tops, font = ("arial", 20, "bold"), text = local_time, fg = "Steel Blue", bd = 10, anchor = "w")
lbl_info.grid(row = 1, column = 0)#TIME

def popup_msg(mode):
    if mode == "exit":
        root = Tk()
        root.geometry("250x75+0+0")
        root.title("Confirm Exit")
        label = Label(root, text="Are You Sure You Want To Exit?")
        label.pack(side="top", fill="x", pady=10)
        B1 = Button(root, text="Yes")
        B1.pack(side = "left")
        B2 = Button(root, text="No", command = root.destroy)
        B2.pack(side = "left")

def btn_Click(numbers):
    global operator
    operator = operator + str(numbers)
    text_Input.set(operator)

def btn_Clear(mode):
    if mode == "all":
        text_Input.set("")
        operator = ""


def btn_Equal():
    global operator
    total_sum = str(eval(operator))#eval evaluates all the ints!
    text_Input.set(total_sum)
    operator = ""

def exit(mode):
    if mode == "exit_all":
        root.destroy

    if mode == "pop":
        popup_msg("exit")

def reset():
    total_bill.set("")
    discount.set("")
    practical_Vol_1.set("")
    practical_Vol_2.set("")
    waves_light.set("")
    properties_matter.set("")
    mech_theory.set("")
    waves_light_theory.set("")
    thermal.set("")
    fields.set("")
    electricity_induction.set("")
    electronics.set("")
    properties_matter_theory.set("")
    matter_radiation.set("")


txtDisplay = Entry(f2, font = ("arial", 20, "bold"), textvariable = text_Input, bd = 30, insertwidth = 4, bg = "green yellow", justify = "right")
txtDisplay.grid(columnspan = 4)

#Calculator Buttons
btn7 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "7", bg = "powder blue", command = lambda : btn_Click(7)) .grid(row = 2, column = 0)
btn8 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "8", bg = "powder blue", command = lambda : btn_Click(8)) .grid(row = 2, column = 1)
btn9 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "9", bg = "powder blue", command = lambda : btn_Click(9)) .grid(row = 2, column = 2)
btn4 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "4", bg = "powder blue", command = lambda : btn_Click(4)) .grid(row = 3, column = 0)
btn5 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "5", bg = "powder blue", command = lambda : btn_Click(5)) .grid(row = 3, column = 1)
btn6 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "6", bg = "powder blue", command = lambda : btn_Click(6)) .grid(row = 3, column = 2)
btn1 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "1", bg = "powder blue", command = lambda : btn_Click(1)) .grid(row = 4, column = 0)
btn2 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "2", bg = "powder blue", command = lambda : btn_Click(2)) .grid(row = 4, column = 1)
btn3 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "3", bg = "powder blue", command = lambda : btn_Click(3)) .grid(row = 4, column = 2)
btn0 = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "0", bg = "powder blue", command = lambda : btn_Click(0)) .grid(row = 5, column = 0)
#Calculator operators
subtracion = Button(f2, padx = 18, pady = 18, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "-", bg = "powder blue", command = lambda : btn_Click("-")) .grid(row = 2, column = 3)
addition = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "+", bg = "powder blue", command = lambda : btn_Click("+")) .grid(row = 3, column = 3)
multiplication = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "*", bg = "powder blue", command = lambda : btn_Click("*")) .grid(row = 4, column = 3)
division = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "/", bg = "powder blue", command = lambda : btn_Click("/")) .grid(row = 5, column = 3)
#Add_price = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "Add To bill", bg = "powder blue", command = lambda : btn_Clear("last")) .grid(row = 6, column = 0)
equals = Button(f2, padx = 16, pady = 16, bd = 8, fg = "black", font = ("arial", 20, "bold"), text = "=", bg = "powder blue", command = lambda : btn_Equal()) .grid(row = 5, column = 3)
clear_all = Button(f2, padx = 10, pady = 10, bd = 8, fg = "black", font = ("arial", 19, "bold"), text = "CA", bg = "powder blue", command = lambda : btn_Clear("all")) .grid(row = 5, column = 1)

#End of Calculator

#Order details
total_bill = StringVar()
discount = StringVar()
practical_Vol_1 = StringVar()
practical_Vol_2 = StringVar()
waves_light = StringVar()
properties_matter = StringVar()
mech_theory = StringVar()
waves_light_theory = StringVar()
thermal = StringVar()
fields = StringVar()
electricity_induction = StringVar()
electronics = StringVar()
properties_matter_theory = StringVar()
matter_radiation = StringVar()

lblIndex = Label(f1, font = ("arial", 16, "bold"), text = "Book", bd = 16, anchor = "w", bg = "powder blue")
lblIndex.grid(row = 0, column = 0)
lblIndex = Label(f1, font = ("arial", 16, "bold"), text = "No. of Books", bd = 16, anchor = "w", bg = "powder blue")
lblIndex.grid(row = 0, column = 1)

lblPractical_Vol_1 = Label(f1, font = ("arial", 16, "bold"), text = "Practical 1", bd = 16, anchor = "w", bg = "powder blue")
lblPractical_Vol_1.grid(row = 1, column = 0)
txtPractical_Vol_1 = Entry(f1, font = ("arial", 16, "bold"), textvariable = practical_Vol_1, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtPractical_Vol_1.grid(row = 1, column = 1)

lblPractical_Vol_2 = Label(f1, font = ("arial", 16, "bold"), text = "Practical 2", bd = 16, anchor = "w", bg = "powder blue")
lblPractical_Vol_2.grid(row = 2, column = 0)
txtPractical_Vol_2 = Entry(f1, font = ("arial", 16, "bold"), textvariable = practical_Vol_2, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtPractical_Vol_2.grid(row = 2, column = 1)

lblWaves_Light = Label(f1, font = ("arial", 16, "bold"), text = "Waves & Light", bd = 16, anchor = "w", bg = "powder blue")
lblWaves_Light.grid(row = 3, column = 0)
txtWaves_Light = Entry(f1, font = ("arial", 16, "bold"), textvariable = waves_light, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtWaves_Light.grid(row = 3, column = 1)

lblPropertiesOfMatter = Label(f1, font = ("arial", 16, "bold"), text = "Properties Of Matter", bd = 16, anchor = "w", bg = "powder blue")
lblPropertiesOfMatter.grid(row = 4, column = 0)
txtPropertiesOfMatter = Entry(f1, font = ("arial", 16, "bold"), textvariable = properties_matter, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtPropertiesOfMatter.grid(row = 4, column = 1)

lblMechanics = Label(f1, font = ("arial", 16, "bold"), text = "Mechanics", bd = 16, anchor = "w", bg = "powder blue")
lblMechanics.grid(row = 5, column = 0)
txtMechanics = Entry(f1, font = ("arial", 16, "bold"), textvariable = mech_theory, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtMechanics.grid(row = 5, column = 1)

lblWaves_Light_Theory = Label(f1, font = ("arial", 16, "bold"), text = "Waves & Light Theory", bd = 16, anchor = "w", bg = "powder blue")
lblWaves_Light_Theory.grid(row = 6, column = 0)
txtWaves_Light_Theory = Entry(f1, font = ("arial", 16, "bold"), textvariable = waves_light_theory, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtWaves_Light_Theory.grid(row = 6, column = 1)

lblThermal_Phy = Label(f1, font = ("arial", 16, "bold"), text = "Thermal Physics", bd = 16, anchor = "w", bg = "powder blue")
lblThermal_Phy.grid(row = 7, column = 0)
txtThermal_Phy = Entry(f1, font = ("arial", 16, "bold"), textvariable = thermal, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtThermal_Phy.grid(row = 7, column = 1)

lblIndex2 = Label(f1, font = ("arial", 16, "bold"), text = "Book", bd = 16, anchor = "w", bg = "powder blue")
lblIndex2.grid(row = 0, column = 2)
lblIndex2 = Label(f1, font = ("arial", 16, "bold"), text = "No. of Books", bd = 16, anchor = "w", bg = "powder blue")
lblIndex2.grid(row = 0, column = 3)

lblFields = Label(f1, font = ("arial", 16, "bold"), text = "Fields", bd = 16, anchor = "w", bg = "powder blue")
lblFields.grid(row = 1, column = 2)
txtFields = Entry(f1, font = ("arial", 16, "bold"), textvariable = fields, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtFields.grid(row = 1, column = 3)

lblElectricity_Induction = Label(f1, font = ("arial", 16, "bold"), text = "Electricity & Induction", bd = 16, anchor = "w", bg = "powder blue")
lblElectricity_Induction.grid(row = 2, column = 2)
txtElectricity_Induction = Entry(f1, font = ("arial", 16, "bold"), textvariable = electricity_induction, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtElectricity_Induction.grid(row = 2, column = 3)

lblElectronics = Label(f1, font = ("arial", 16, "bold"), text = "Electronics", bd = 16, anchor = "w", bg = "powder blue")
lblElectronics.grid(row = 3, column = 2)
txtElectronics = Entry(f1, font = ("arial", 16, "bold"), textvariable = electronics, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtElectronics.grid(row = 3, column = 3)

lblPropertiesOfMatter_Theory = Label(f1, font = ("arial", 16, "bold"), text = "Properties Of Matter Theory", bd = 16, anchor = "w", bg = "powder blue")
lblPropertiesOfMatter_Theory.grid(row = 4, column = 2)
txtPropertiesOfMatter_Theory = Entry(f1, font = ("arial", 16, "bold"), textvariable = properties_matter_theory, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtPropertiesOfMatter_Theory.grid(row = 4, column = 3)

lblMatterAndRadiation = Label(f1, font = ("arial", 16, "bold"), text = "Matter and Radiation", bd = 16, anchor = "w", bg = "powder blue")
lblMatterAndRadiation.grid(row = 5, column = 2)
txtMatterAndRadiation = Entry(f1, font = ("arial", 16, "bold"), textvariable = matter_radiation, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtMatterAndRadiation.grid(row = 5, column = 3)

lblDiscount = Label(f1, font = ("arial", 16, "bold"), text = "Discount", bd = 16, anchor = "w", bg = "powder blue")
lblDiscount.grid(row = 6, column = 2)
txtDiscount = Entry(f1, font = ("arial", 16, "bold"), textvariable = discount, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtDiscount.grid(row = 6, column = 3)

lblTotal = Label(f1, font = ("arial", 16, "bold"), text = "Total", bd = 16, anchor = "w", bg = "powder blue")
lblTotal.grid(row = 7, column = 2)
txtTotal = Entry(f1, font = ("arial", 16, "bold"), textvariable = total_bill, bd = 10, insertwidth = 4, bg = "powder blue", justify = "right")
txtTotal.grid(row = 7, column = 3)

#Order buttons
btnTotal = Button(f1, padx = 16, pady = 8, bd = 16, fg = "black", font = ("arial", 16, "bold"), width = 10, text = "Total", bg = "powder blue") .grid(row = 8, column = 0)

btnReset = Button(f1, padx = 16, pady = 8, bd = 16, fg = "black", font = ("arial", 16, "bold"), width = 10, text = "Reset", bg = "powder blue", command = reset()) .grid(row = 8, column = 1)

btnExit = Button(f1, padx = 16, pady = 8, bd = 16, fg = "black", font = ("arial", 16, "bold"), width = 10, text = "Exit", bg = "powder blue", command = exit("pop")) .grid(row = 8, column = 2)

root.mainloop()
If I am correct, The popup will only trigger when I press exit.
But why does it happen when I run it? Without pressing exit.


Also, I want to know,
How can I make 2 widows close in 1 command,
def popup_msg(mode):
    if mode == "exit":
        root = Tk()
        root.geometry("250x75+0+0")
        root.title("Confirm Exit")
        label = Label(root, text="Are You Sure You Want To Exit?")
        label.pack(side="top", fill="x", pady=10)
        B1 = Button(root, text="Yes")
        B1.pack(side = "left")
        B2 = Button(root, text="No", command = root.destroy)
        B2.pack(side = "left")
When I click yes, it should close both the windows.
Now it closes only 1(The PopUp)
Reply
#2
It pops up because you call exit("pop") when you make the button.
btnExit = Button(f1, padx = 16, pady = 8, bd = 16, fg = "black",
                 font = ("arial", 16, "bold"), width = 10,
                 text = "Exit", bg = "powder blue",
                 command = exit("pop")) .grid(row = 8, column = 2)  # <- Calling exit("pop")
There are several problems here. The first is you are calling exit("pop") when you make the button. The second is that command=None because exit("pop") returns None when called. The third is that btnExit is also None. After you make the Button you call .grid(row=8, column=2). .grid() returns None, so btnExit is None. And the final problem is that you have a lot of lines that are too long. Your programs will be easier to work with if all you lines are less than 80 characters long.

You probably want you code to look more like this:
btnExit = Button(f1, padx=16, pady=8, bd = 16, fg = "black",
                 font = ("arial", 16, "bold"), width = 10,
                 text = "Exit", bg = "powder blue",
                 command = lambda: exit("pop"))
btnExit.grid(row = 8, column = 2)
Instead of a lambda you could also use a functools.partial, or you could split the exit() function in half.
def exit_program:
    root.destroy
 
def verify_exit():
    popup_msg("exit")
I would have the verify_exit() function create a popup dialog. You should only ever call Tk() once in a program. Read about tkinter dialogs here:

https://docs.python.org/3.9/library/dialog.html
Reply
#3
How to make the dialog buttons come in the center bottom.
Instead of going to left/right or stacking
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt trigger leaveEvent in parent kainev 5 4,698 Dec-03-2019, 04:22 PM
Last Post: Denni

Forum Jump:

User Panel Messages

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