Updating a Tkinter Image Button trough user input
Question:
I am trying to update the image from an image button through file dialog. The input from the user should be saved in a text file and then applied to the image button.
#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
import time
#define the first tk window
window = Tk()
window.geometry("655x600")
window.title("test")
window.resizable(False, False)
window.config(background="#2e3033")
global file12
with open("data12.txt", 'r') as file12:
imgdata12 = file12.read().replace('n', '')
def save_data12(data):
with open("data12.txt", "w", encoding="utf-8") as file:
file.write(data)
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)
def browser12():
filepath = filedialog.askopenfilename(initialdir=os.getcwd(), title="select an Image File" ,filetypes=(("PNG File","*.png"),("JPG File", "*.jpg")))
with open(".txt", "w", encoding="utf-8") as file12:
file12.write(filepath)
change()
def change():
file12.close()
with open("data/image_input/data12.txt", 'r') as file:
imgdata12 = file.read().replace('"', '')
streamdex_button12.place_forget()
streamdex_image13 = PhotoImage(file=imgdata12)
streamdex_button13 = Button(window, text="hello" , command=new_window12 , image=streamdex_image12)
streamdex_button13.place(x=500,y=380)
print("test")
window.mainloop()
Answers:
You just had to global the image like this:
def browser4():
filepath4 = filedialog.askopenfilename(initialdir=os.getcwd(), title="select an Image File" ,filetypes=(("PNG File","*.png"),("JPG File", "*.jpg")))
with open("data/image_input/data4.txt", "w", encoding="utf-8") as file:
file.write(filepath4)
global streamdex_image4
streamdex_image4.config(file=filepath4)
I am trying to update the image from an image button through file dialog. The input from the user should be saved in a text file and then applied to the image button.
#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
import time
#define the first tk window
window = Tk()
window.geometry("655x600")
window.title("test")
window.resizable(False, False)
window.config(background="#2e3033")
global file12
with open("data12.txt", 'r') as file12:
imgdata12 = file12.read().replace('n', '')
def save_data12(data):
with open("data12.txt", "w", encoding="utf-8") as file:
file.write(data)
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)
def browser12():
filepath = filedialog.askopenfilename(initialdir=os.getcwd(), title="select an Image File" ,filetypes=(("PNG File","*.png"),("JPG File", "*.jpg")))
with open(".txt", "w", encoding="utf-8") as file12:
file12.write(filepath)
change()
def change():
file12.close()
with open("data/image_input/data12.txt", 'r') as file:
imgdata12 = file.read().replace('"', '')
streamdex_button12.place_forget()
streamdex_image13 = PhotoImage(file=imgdata12)
streamdex_button13 = Button(window, text="hello" , command=new_window12 , image=streamdex_image12)
streamdex_button13.place(x=500,y=380)
print("test")
window.mainloop()
You just had to global the image like this:
def browser4():
filepath4 = filedialog.askopenfilename(initialdir=os.getcwd(), title="select an Image File" ,filetypes=(("PNG File","*.png"),("JPG File", "*.jpg")))
with open("data/image_input/data4.txt", "w", encoding="utf-8") as file:
file.write(filepath4)
global streamdex_image4
streamdex_image4.config(file=filepath4)