user11322796
user11322796

Reputation:

can't iterate loop in tkinter label

The problem: I am trying to pull a list of results from the freshdesk api website. It all comes in json and I have to use a for loop to retrieve the specific data I want.

When I use the for loop and use "print" it works and I see all the information I want in the IDE's command line.

Example:

ticketresponse = requests.get(freshdeskticketsurl, auth = (freshdeskapi, freshdeskpassword))
ticket_json = ticketresponse.json()
lenghtofticket30 = (len(ticket_json))
print(f"Here are the number of tickets in the past 30 days: ", lenghtofticket30)

def myTicketstatusloop():
    for i in range(len(ticket_json)):
        ticketsubject_str = json.dumps(ticket_json[i]['subject'], indent=2,)
        ticketstatus_str = json.dumps(ticket_json[i]['status'], indent=2,)
        ticketcreationdate = json.dumps(ticket_json[i]['created_at'], indent=2,)
        ticketupdateddate = json.dumps(ticket_json[i]['updated_at'], indent=2)
        ticketstatusreplace_str = ticketstatus_str.replace("2", "Open")
        ticketstatusreplace_str = ticketstatus_str.replace("5", "Closed")
        print(ticketsubject_str)
        print(ticketstatusreplace_str)
        print(ticketcreationdate)
        print(ticketupdateddate)
        print("\n")

myTicketstatusloop()

Result:

Here are the number of tickets in the past 30 days:  38
"Ensure M365 email is in outlook, setup wifi adapter, see why computer is slow"
Closed
"2021-03-03T20:08:54Z"
"2021-03-03T20:09:00Z"


"FW: Email DL List"
Closed
"2021-03-03T19:21:16Z"
"2021-03-03T19:28:29Z"


"knowb4 users"
2
"2021-03-03T17:38:39Z"
"2021-03-03T17:38:39Z"


"FW: EFT Payment scheduled 03 March 21 Account: MJJ0011A"
2
"2021-03-03T17:30:36Z"
"2021-03-03T17:35:34Z"

Picture:

However when I try to get the same value inside a label for tkinter, I only get the first result.

Example:

ticketresponse = requests.get(freshdeskticketsurl, auth = (freshdeskapi, freshdeskpassword))
ticket_json = ticketresponse.json()
lenghtofticket30 = (len(ticket_json))
print(f"Here are the number of tickets in the past 30 days: ", lenghtofticket30)

def myTicketstatusloop():
    for i in range(len(ticket_json)):
        ticketsubject_str = json.dumps(ticket_json[i]['subject'], indent=2,)
        ticketstatus_str = json.dumps(ticket_json[i]['status'], indent=2,)
        ticketcreationdate = json.dumps(ticket_json[i]['created_at'], indent=2,)
        ticketupdateddate = json.dumps(ticket_json[i]['updated_at'], indent=2)
        ticketstatusreplace_str = ticketstatus_str.replace("2", "Open")
        ticketstatusreplace_str = ticketstatus_str.replace("5", "Closed")
        label1 = Label(homepage, text = ticketsubject_str + "\n" + ticketstatusreplace_str + "\n" + ticketcreationdate + "\n" + ticketupdateddate)
        label1.grid(row=1, column=1)

homepage = Tk()
homepage.title("My first GUI")
# set size of window
homepage.geometry('1473x400')


    
    
myButton1 = Button(homepage, text="Tickets open", command=myTicketstatusloop, bg="black", fg="green", font='Helvetica 13 bold', padx=20, pady=20).grid(row=0, column=0)
myButton2 = Button(homepage, text="Tickets closed today", bg="black", fg="green", font='Helvetica 13 bold',padx=20, pady=20).grid(row=0, column=1)
myButton3 = Button(homepage, text="most reoccuring type of ticket", bg="black", fg="green", font='Helvetica 13 bold', padx=20, pady=20).grid(row=0, column=2)
myButton4 = Button(homepage, text="Average Hours this week", bg="black", fg="green", font='Helvetica 13 bold', padx=20, pady=20).grid(row=0, column=3)
myButton5 = Button(homepage, text="Todo-list", bg="black", fg="green", font='Helvetica 13 bold', padx=20, pady=20).grid(row=0, column=4)
myButton6 = Button(homepage, text="Intermedia", bg="black", fg="green", font='Helvetica 13 bold', padx=20, pady=20).grid(row=0, column=5)
myButton7 = Button(homepage, text="M365", bg="black", fg="green", font='Helvetica 13 bold', padx=20, pady=20).grid(row=0, column=6)
myButton8 = Button(homepage, text="Knowbe4", bg="black", fg="green", font='Helvetica 13 bold', padx=20, pady=20).grid(row=0, column=7)
myButton9 = Button(homepage, text="Ninja", bg="black", fg="green", font='Helvetica 13 bold', padx=20, pady=20).grid(row=0, column=8)


homepage.mainloop()

Result:

"FW: Alert - OnPointe Properties / JMark and Associates - CPDC02 - Disk Volume Free space for 'C:' is less than or equal to 10% from 2021-0..."
Closed
"2021-03-03T16:45:29Z"
"2021-03-03T19:20:45Z"

Picture:

I tried a couple of different ideas but nothing so far gives me all the data. If you have any ideas or suggestions let me know.

Thanks everyone!!

Upvotes: 0

Views: 38

Answers (1)

Delrius Euphoria
Delrius Euphoria

Reputation: 15098

Quick fix: If you want to display it all downwards, then just increase the row number, like:

label1.grid(row=i, column=1)

If you want different structure, then append all this pulled data to a list and then loop through the required row and columns, taking item from the list.

Upvotes: 1

Related Questions