0

I'm trying to show an image multiple times after several button presses, though once I've pressed the button once, and altered the entry input, it no longer updates.

Code:

 import tkinter as tk 
 import qrcode 
 from PIL import ImageTk,Image
 root = tk.Tk() 
 root.geometry('300x300')
 root.resizable(False,False)
 root.title('QR-Code Maker')

 def create_qrcode(data:str):
     qrc = qrcode.make(data,box_size=10)
     return qrc

 def buttoncallback(entry):
     data_for_qrcode = entry.get()
     qrc = create_qrcode(data_for_qrcode)
     qrc.save('QRCODE.PNG')

     img = ImageTk.PhotoImage(Image.open('QRCODE.PNG'))
 
     panel = tk.Label(root,image=img)
     panel.pack(side='bottom',fill='both',expand='yes')
 
     root.mainloop()

 def main():
     entry = tk.Entry(root)
     entry.pack(anchor=tk.CENTER)
 
     button = tk.Button(root,text='Genereer QR',command=lambda:buttoncallback(entry))
     button.pack(anchor=tk.CENTER)
     root.mainloop()

 if __name__ == '__main__':
     main()

I'm sure I've missed something incredibly obvious, but I cannot for the life of me figure it out

mewenbonk
  • 7
  • 3
  • 1
    Every time you create a new image, you add it to the bottom of the window - which is of a fixed size. I'm sure all of the images are actually being created, they're just being pushed off the bottom of the window, where you can't see them. You either need to delete the previous image when you create the new one, or have a single Label from the very start that you simply assign the new image to. – jasonharper Mar 21 '23 at 15:17
  • I've made the root resizable, the root now expands but isn't showing any image. – mewenbonk Mar 21 '23 at 15:23
  • 1
    You need to save a reference to the image somewhere for as long as it's being displayed, to keep it from being garbage-collected. `panel.img = img` would be one approach. – jasonharper Mar 21 '23 at 15:27
  • How would I go about deleting the previous QR code and putting the new one in the previous' place? – mewenbonk Mar 21 '23 at 15:32
  • 1
    This looks like a duplciate of [Why does Tkinter image not show up if created in a function?](https://stackoverflow.com/questions/16424091) – Bryan Oakley Mar 21 '23 at 15:36
  • @BryanOakley Sounds right...we really ought to have something like a 'sticky' for this answer. – JRiggles Mar 21 '23 at 15:59

1 Answers1

1

You have two calls to root.mainloop() - you should only have one (specifically, the one in main()). Remove the call to root.mainloop() from buttoncallback()

 def buttoncallback(entry):
     data_for_qrcode = entry.get()
     qrc = create_qrcode(data_for_qrcode)
     qrc.save('QRCODE.PNG')

     img = ImageTk.PhotoImage(Image.open('QRCODE.PNG'))
 
     panel = tk.Label(root,image=img)
     panel.pack(side='bottom',fill='both',expand='yes')
 
     # root.mainloop()  <-- remove this
JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • I've tried that, but then it won't show on even the first time. How do I get it to update without a root.mainloop? – mewenbonk Mar 21 '23 at 15:17
  • You can call `root.update_idletasks()` to refresh the UI, but I suspect @jasonharper is correct about what's happening to your images – JRiggles Mar 21 '23 at 15:20