0

Here's my problem: There's a database of addresses for companies - if you try to create an invoice and the address for it is missing it creates the pop up prompting entering the address. However once that's done the rest of the code is never executed. Not sure if it's got something to do with the new window or the try/except statement.

def addPopUp(table):
    def popClose():
        print("closing")
        popup2.destroy()

    def addAddress(table, address):
        file = open('addresses.csv', 'a')
        file.write("\n{},{}".format(table[3], address))
        file.close()
        popup2.destroy()


    popup2 = Tk()
    popup2.wm_title("Add address")
    label = Label(popup2, text="Address needed for {}".format(table[3]))
    label.grid(row=0, column=0, columnspan=3)
    newAdd = Entry(popup2)
    newAdd.grid(row=1, column=0, columnspan=3,)
    addBut = Button(popup2, text="Add", command= lambda:addAddress(table,newAdd.get()))
    addBut.grid(row=2,column=0)
    cancel = Button(popup2, text="Cancel",command = popClose)
    cancel.grid(row=2, column=2)

    popup2.mainloop()

def generateInvoice():

        print("Generating invoice")

        sel = invoices.selection()
        if  sel == ():
            print("You need to select a line")
            return
        line = int(sel[0][1::])-1

        global addresses
        if True:
            try:
                addresses[invoiceData[line][3]] == None

            except:
                addPopUp(invoiceData[line])
        # THIS CODE NEVER HAPPENS IF THERE'S AN EXCEPTION vvvvvvvv
                addresses = loadAddresses()



        def addPO():
            newPO = poNum.get()
            if newPO != "":
                print(invoiceData[line])
                invoiceData[line].pop(7)
                invoiceData[line].insert(7,newPO)
                print(invoiceData[line])



            popup.destroy()
            createPdf(invoiceData[line],addresses)
        def notNeeded():

            popup.destroy()
            createPdf(invoiceData[line],addresses)

        def popClose():
            print("closing")
            popup.destroy
        if invoiceData[line][7]=='':
            popup = Tk()
            popup.wm_title("PO needed?")
            label = Label(popup,text = "Add PO")
            label.grid(row=0,column=0,columnspan = 3)
            poNum = Entry(popup)
            poNum.grid(row=1,column=0,columnspan = 3)
            addBut = Button(popup, text = "Add", command=addPO)
            noNeed = Button(popup,text = "Not needed", comman=notNeeded)
            addBut.grid(row=2,column=0)
            noNeed.grid(row=2,column=1)
            cancel = Button(popup,text = "Cancel", command=popClose)
            cancel.grid(row=2,column=2)

            popup.mainloop()
        else:
            createPdf(invoiceData[line],addresses)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    You haven't provided a [mre]. Regardless, you shouldn't call `Tk()` more than once when using `tkinter`. If you want multiple windows, call [`Toplevel()`](https://web.archive.org/web/20190429194251id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/toplevel.html) to create them instead. – martineau Nov 25 '19 at 10:28
  • Thanks. I'll try that. – Andrew Zmurowski Nov 25 '19 at 10:44
  • Note `Tk()` needs to be call at least once. This instance is commonly named `root` and represents the main application's window. – martineau Nov 25 '19 at 10:48
  • @AndrewZmurowski: Read [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) and [Best way to structure a tkinter application](https://stackoverflow.com/a/17470842/7414759) – stovfl Nov 25 '19 at 10:51
  • Thanks for help all even though I didn't provide a working example. Turned out the code works with popup2.quit() – Andrew Zmurowski Nov 25 '19 at 19:21

0 Answers0