Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

python 3.x - Tkinter how to create new labels after mainloop()

I am trying to make a tkinter application which will allow the user to enter music album details and save it to a json file. Ive ran into a problem where the program can read the file when it is first ran and display everything but when the user adds a new album after root.mainloop() has ran,I cant seem to get it to display the new albums unless i restart the program where root.mainloop() hasn't ran yet.

def update():
    for i in range(len(album_list)):
        lbl_album = tk.Label(root, text=album_list[i]["name"])
        lbl_artist = tk.Label(root, text=album_list[i]["artist"])
        lbl_year = tk.Label(root, text=album_list[i]["year"])
        lbl_genre = tk.Label(root, text=album_list[i]["genre"])

        row_num = i + 2
        lbl_album.grid(row=row_num, column=1)
        lbl_artist.grid(row=row_num, column=2)
        lbl_year.grid(row=row_num, column=3)
        lbl_genre.grid(row=row_num, column=4)


update()

root.mainloop()
def confirm():
        file = open(file_address, 'r')
        contents = json.loads(file.read())
        file.close()

        contents.append({
            "name": ent_album.get(),
            "artist": ent_artist.get(),
            "year": ent_year.get(),
            "genre": ent_genre.get()
        })

        contents_json = json.dumps(contents)
        file = open(file_address, 'w')
        file.write(contents_json)
        file.close()

        update()

The confirm method is ran after the user clicks on the add button. It is able to write to the json but i cant get it to either read the updated version and rewrite the whole table or just to add it onto the end

question from:https://stackoverflow.com/questions/65831235/tkinter-how-to-create-new-labels-after-mainloop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...