my problem is that I don't know how to update an image button the location for the image is stored in a text file but even if I reopen the file the button does not update I also tried deleting the button and adding it back but that also didn't work thanks for your help and time.
#imports
from ast import Lambda
from cProfile import label
from fileinput import filename
from tkinter import Entry, filedialog
from tkinter import *
import os
import shutil
from PIL import ImageTk , Image
import time
#define the first tk window
window = Tk()
window.geometry("655x600")
window.title("streamdex")
window.resizable(False, False)
icon = PhotoImage(file="data/icons/venkuicon.png")
window.iconphoto(True,icon)
window.config(background="#2e3033")
def save_data12(data):
with open("data/userinput/data12.txt", "w", encoding="utf-8") as file:
file.write(data)
global file12
with open("data/image_input/data12.txt", 'r') as file12:
imgdata12 = file12.read().replace('\n', '')
def new_window12():
window13 = Toplevel()
window13.geometry("500x100 200 300")
window13.config(background="#2e3033")
window13.title("Edit Button 12")
Button12 = Button(window13, text="save",command=lambda: [save_data12(entry12.get()),window13.destroy()])
Button12.place(x=5,y=70)
entry12 = Entry(window13,width=100, font= ("Arial",12))
entry12.place(x=5,y=30)
browse_button12 = Button(window13, text="Change image", command=lambda: [browser12(), window13.destroy()])
browse_button12.place(x=50, y=70)
streamdex_image12 = PhotoImage(file=imgdata12)
streamdex_button12 = Button(window, text="hello" , command=new_window12 , image=streamdex_image12)
streamdex_button12.place(x=500,y=380)
file12.close()
with open("data/image_input/data12.txt", 'r') as file:
imgdata12 = file.read().replace('"', '')
def change():
streamdex_button12.place_forget()
time.sleep(0.1)
streamdex_button12.place(x=500,y=380)
def browser12():
filepath = filedialog.askopenfilename(initialdir=os.getcwd(), title="select an Image File" ,filetypes=(("PNG File","*.png"),("JPG File", "*.jpg")))
with open("data/image_input/data12.txt", "w", encoding="utf-8") as file:
file.write(filepath)
change()
window.mainloop()
CodePudding user response:
If this is the code you are referring to, it only executes when the program starts because of the indent. Although it will read the same thing if run a second time. There is no way to tell which part of the program you mean based on the description.
global file12
with open("data/image_input/data12.txt", 'r') as file12:
imgdata12 = file12.read().replace('\n', '')
CodePudding user response:
If there aren't a huge number of images, load them into a list at the beginning and loop through the list. The following program appends all images into a list. Click on the image to switch to the next. Small icons are used so the images are small enough to be in the source code. You would, of course, be reading files.
import tkinter as tk ## Python 3.x
class ChangeImage():
def __init__(self, root):
self.photos=[]
self.load_images()
self.image_num=0
self.btn = tk.Button(root, image=self.photos[self.image_num], command=self.next_image)
self.btn.grid(row=0)
tk.Button(root, text="Exit", bg="orange", command=root.quit).grid(row=1)
def next_image(self):
self.image_num = 1
if self.image_num >= len(self.photos):
self.image_num=0
## pipe the next image to be displayed to the button
self.btn["image"]=self.photos[self.image_num]
def load_images(self):
""" copy data images to a list that is an instance variable
all images are hard-coded here and so use data=
instead of file=
"""
ladybug_gif_b64="""
R0lGODlhIAAgALMAAP///wAAADAwMP99Hf8AAP lAP//AMopUwAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAACH5BAAAAAAALAAAAAAgACAAAwTHEMhJq714hp3lDh0GiqH2UWOVAt96pUIsBLKglWg87Dwv
4xMBj0Asxgg XKxwLBJrxUGsI5TKnARoVHoLDp5XbNP5cwmNAqwa/aOc13ByrfKOw2UGw1SSxrb
AWIxeXsAaX92UDQ1Nh6BdneMhQIHkHGSjYaVlmt4epmacp19YAKEoJRspKWrjBapWWGqcm1uB5tj
ok4HZa 3m5wEt5kuhpTAcb FVL/NzspAfDHPysslMJjEIS0oLygnOMVd0SwcHeDk6errEQA7"""
grape_gif="""
R0lGODlhIAAgALMAAAAAAAAAgHCAkC6LV76 vvXeswD/ANzc3DLNMubm v/6zS9PT6Ai8P8A////
/////yH5BAEAAAkALAAAAAAgACAAAAS00MlJq7046803AF3ofAYYfh8GIEvpoUZcmtOKAO5rLMva
0rYVKqX5IEq3XDAZo1GGiOhw5rtJc09cVGo7orYwYtYo3d4 DBxJWuSCAQ30 vNTGcxnOIARj3eT
YhJDQ3woDGl7foNiKBV7aYeEkHEignKFkk4ciYaImJqbkZ PjZUjaJOElKanqJyRrJyZgSKkokOs
NYa2q7mcirC5I5FofsK6hcHHgsSgx4a9yzXK0rrV19gRADs="""
house="""
R0lGODdhFQAVAPMAAAQ2PESapISCBASCBMTCxPxmNCQiJJya/ISChGRmzPz /PxmzDQyZDQyZDQy
ZDQyZCwAAAAAFQAVAAAElJDISau9Vh2WMD0gqHHelJwnsXVloqDd2hrMm8pYYiSHYfMMRm53ULlQ
HGFFx1MZCciUiVOsPmEkKNVp3UBhJ4Ohy1UxerSgJGZMMBbcBACQlVhRiHvaUsXHgywTdycLdxyB
gm1vcTyIZW4MeU6NgQEBXEGRcQcIlwQIAwEHoioCAgWmCZ0Iq5 hA6wIpqislgGhthEAOw=="""
smiley = """
R0lGODlhEAAQAOeSAKx7Fqx8F61/G62CILCJKriIHM HALKNMNCIANKKANOMALuRK7WOVLWPV9eR
ANiSANuXAN2ZAN6aAN bAOCcAOKeANCjKOShANKnK imAOyrAN6qSNaxPfCwAOKyJOKyJvKyANW0
R/S1APW2APW3APa4APe5APm7APm8APq8AO28Ke29LO2/LO2/L 7BM 7BNO6 Re7CMu7BOe7DNPHA
P /FOO/FO jGS /FQO/GO/DHPOjBdfDIPPDJQPDISPDKQPDKRPDIUPHLQ/HLRerMV/HMR/LNSOvH
fvLOS/rNP/LPTvLOVe/LdfPRUfPRU/PSU/LPaPPTVPPUVfTUVvLPe/LScPTWWfTXW/TXXPTXX/XY
Xu/SkvXZYPfVdfXaY/TYcfXaZPXaZvbWfvTYe/XbbvHWl/bdaPbeavvadffea/bebvffbfbdfPvb
e/fgb/Pam/fgcvfgePTbnfbcl/bfivfjdvfjePbemfjelPXeoPjkePbfmvffnvbfofjlgffjkvfh
nvjio/nnhvfjovjmlvzlmvrmpvrrmfzpp/zqq/vqr/zssvvvp/vvqfvvuPvvuvvwvfzzwP//////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////yH FUNyZWF0ZWQgd2l0aCBU
aGUgR0lNUAAh QQBC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Nj
TCdu5MARQ5CKGTZ yNjQIUOBDzpooEgUggSIRCVEDAoEADs="""
## put the images in an instance object (self.) so they aren't destroyed
## when the function exits and can be used anywhere in the class
for photo in (ladybug_gif_b64, grape_gif, house, clock, smiley):
self.photos.append(tk.PhotoImage(data=photo))
root=tk.Tk()
root.geometry(" 50 50")
CI=ChangeImage(root)
root.mainloop()