Jun-05-2020, 02:25 AM
I've searched and experimented but am unable to solve my problem with canvas. I have been teaching myself Python. I first wrote a text program to test my knowledge of lists, dictionaries, etc. The program runs well. It's not a useful program but does provide me practice in doing various things. I then spent time converting it to a GUI. The GUI version works well (from my perspective); however, one issue, a minor one but it's bugging me, still exists.
I create my window and then I create a canvas. Inside the canvas, place an image. That all works fine. The problem comes when I call a method from a class - the method works just fine but the image disappears. I actually have two classes - the super class that performs actions relative to adding, editing, deleting records, and a method (here) displaying the dictionary records and a subclass that performs the menubar. Here's some of the code:
Thanks,
Nick.
I create my window and then I create a canvas. Inside the canvas, place an image. That all works fine. The problem comes when I call a method from a class - the method works just fine but the image disappears. I actually have two classes - the super class that performs actions relative to adding, editing, deleting records, and a method (here) displaying the dictionary records and a subclass that performs the menubar. Here's some of the code:
class People: # abbreviated class; only showing two methods def __init(self): self.frame = Frame(window def view_records(self): self.frame.destroy() self.frame = Frame(window) self.frame.configure(bg='light green') self.frame.grid(row=0, column=0) """ I tried recreating the canvas and that didn't work. """ # canvas = Canvas(window, bg='light green', width=245, height=245) # image = ImageTk.PhotoImage(Image.open("picture.png")) # open the Army file # canvas.create_image(0, 0, anchor=NW, image=image) # draw the image current_row = 10 for key in self.people: person = 'Record: ' + str(key) + str(self.people[key]) label = Label(self.frame, text=person, bg='light green', font=('Helvetica', 8, 'bold')) label.grid(row=current_row, column=2, sticky=W) current_row += 1 window = Tk() window.title("People System") window.geometry("750x300+325+200") window.configure(bg='light green') canvas = Canvas(window, bg='light green', width=245, height=245) image = ImageTk.PhotoImage(Image.open("picture.png")) # open the Army file canvas.create_image(0, 0, anchor=NW, image=image) # draw the image """ padx and pady do put the window on the far right if I adjust 'padx'; however, if I run 'view_records', the image disappears. """ canvas.grid(row=0, column=3, padx=500, pady=0, sticky=W) people = CreateMenu() people.view_records() window.mainloop()Any help would be greatly appreciated.
Thanks,
Nick.