Here is a part of my code, which loads images in a LabelFrame based on JSON objects from an external file. The code is inside a function (seems like that plays a role too, who knows).
# the black blackboard where images will be stored
black_background = tk.LabelFrame(pivote_tab, width=1340, height=290, bg="black")
black_background.grid(row=0, column=0, sticky="E")
# open the .json file
json_file = open('path/to/file.json', 'r')
json_data = json_file.read()
#the init position for image inside the blackboard
live_pc_position = 10
# make a loop for every object inside JSON
obj = json.loads(json_data)
for i in range(int(len(obj))):
os = (str(obj[i].get('operating_system')))
if "Mac" in os:
img_src = "image/macos_pc.png"
elif "Win" in os and "10" in os:
img_src = "image/windows_10.png"
elif "Linux" in os:
img_src = "image/linux_pc_image.png"
else:
img_src = "image/undetected_pc.png"
# defining the image based on operating system and store it to the pc_image variable
pc_image = PhotoImage(file=img_src)
# adding the computer icon inside the blackboard
computer_icon = Label(black_background, image=pc_image, bg="black")
computer_icon.place(x=live_pc_position, y=10)
# update values so the next image will load to the right
live_pc_position = live_pc_position + 40
The script doesn't make any error, however for some reason only the first image is displayed when there are more images expected to be loaded, because JSON has more objects.
This is what I was suggesting in my comment. See ALL CAPS COMMENT.
obj = json.loads(json_data)
for i in range(int(len(obj))):
os = (str(obj[i].get('operating_system')))
...
# defining the image based on operating system and store it to the pc_image variable
pc_image = PhotoImage(file=img_src)
# adding the computer icon inside the blackboard
computer_icon = Label(black_background, image=pc_image, bg="black")
computer_icon.img = pc_image # KEEP A REFERENCE SO IMAGE IS NOT GARBAGE COLLECTED.
computer_icon.place(x=live_pc_position, y=10)
...
The problem with image is not loading in tkinter is almost always related to this.
At every loop you replace the value in pc_image and python garbage collector discards the Tkinter image object. The solution is to storage each Tkinter object in a different variable, which can be done with an object attribute or a list like the example bellow:
pc_images=[]
for i in range(int(len(obj))):
...
pc_images.append(PhotoImage(file=img_src))
Label(black_background, image=pc_images[i], bg="black").place(x=live_pc_position, y=10)
live_pc_position = live_pc_position + 40
The script is from #flavio-moraes logically works, however with a mix of the answers I got, I made the final script which fullfills my needs.
pc_images=[]
for i in range(int(len(obj))):
...
pc_images.append(PhotoImage(file=img_src))
live_pc_position = 10
for pc in pc_images:
#create the image
computer_icon = Label(black_background, image=pc, bg="black")
computer_icon.img = pc_images
computer_icon.place(x=live_pc_position, y=10)
# update values so the next image will load to the right
live_pc_position = live_pc_position + 40
Related
I would like to make a window construction for a photo previewer. I am stuck. My code doesn't work.
I made a frame hierarchy shown below.
I would like to show thumbnails in the thumbnail frame.
# TODO 0: IMPORT REQUIRED LIBRARIES AND MODULES
from tkinter import *
from PIL import Image, ImageTk
# TODO 1: CREATE A CLASS
class EditScreen:
def __init__(self, file_directory_list):
self.edit_window_init()
self.file_directory_list = file_directory_list
self.display_selected_images()
self.edit_window.mainloop()
# TODO 2: CREATE A NEW EDIT SCREEN
def edit_window_init(self):
self.edit_window = Toplevel()
self.edit_window.title("Edit")
self.edit_window.geometry('1200x800')
self.background_color = "#F8F1F1"
self.my_font_color = "#0A065D"
self.my_font = 'Poppins'
# TODO 3: DISPLAY SELECTED IMAGES IN THIS SCREEN
def display_selected_images(self):
thumbnail_frame = Frame(self.edit_window, height=1200, width=300)
thumbnail_frame.pack(side='left')
thumbnail_frame_canvas= Canvas(master=thumbnail_frame)
thumbnail_frame_canvas.place(x=0,y=0)
# TODO 3.1: PUT ALL SELECTED IMAGES THUMBNAIL TO LEFT HAND SIDE OF THE EDIT SCREEN
for selected_image_directory in self.file_directory_list:
print(self.file_directory_list.index(selected_image_directory))
selected_image = ImageTk.PhotoImage(file=selected_image_directory)
selected_image_location = thumbnail_frame_canvas.create_image((30,30),image=selected_image)
selected_image_location.pack()
#.grid(row=self.file_directory_list.index(selected_image_directory),column=0)
Consider these lines of code:
selected_image_location = thumbnail_frame_canvas.create_image((30,30),image=selected_image)
selected_image_location.pack()
create_image is documented to return an integer, which is an internal id of the canvas object that was created. You then try to call pack on that integer, but integers don't have a pack method. You need to remove the call to pack
Another problem was mentioned in a comment to your question: you're not saving a reference to the image, so it's going to get destroyed by python's garbage collector.
You can save a reference by using a global list, and appending the images to that list:
def display_selected_images(self):
global selected_images
...
selected_images = []
for selected_image_directory in self.file_directory_list:
...
selected_image = ImageTk.PhotoImage(file=selected_image_directory)
selected_images.append(selected_image)
...
I have been trying to use tkinter CheckButton widget to edit items in a list - each item of the list is a new checkbutton. I want a save method to save the data to a text file, and a load method to load the info from the text file and mark the checkbutton boxes depending on the items in the list.
Here is my code so far, but the list doesn't seem to change when I check the buttons and update the list/file
Here is my code, I need to know why the list isn't updating when I check the boxes:
import tkinter.messagebox as box
modulesMain = Tk()
moduleChecks = []
def SaveChanges():
# Clear the text file
modules = open("modules.txt", "w") #Write mode to overwrite the whole file
modules.write("") # Put in blank text
modules.close()
modules = open("modules.txt", "a") # Append mode to append the file
for item in moduleChecks:
modules.write(item + "\n")
print(moduleChecks)
appButton = Checkbutton(modulesMain, text = "Test", variable = moduleChecks[0]).grid()
searchButton = Checkbutton(modulesMain, text = "Test", variable = moduleChecks[1]).grid()
Save = Button(modulesMain, text = "Save Changes", command = SaveChanges).grid()
The variable for each of your checkboxes needs to be an IntVar. Your moduleCheck list is currently un-initialised so when you try to access the elements inside it, you'll get an error.
In the below code (modified from yours), I've initialised the moduleCheck to contain two IntVars.
When you press the save button, it will print to the console the current state of the check boxes.
import tkinter as tk
modulesMain = tk.Tk()
moduleChecks = [tk.IntVar() for i in range(2)]
def SaveChanges():
for idx,item in enumerate(moduleChecks):
print(f"{idx} = {item.get()}")
appCheck = tk.Checkbutton(modulesMain, text = "App", variable = moduleChecks[0])
appCheck.grid()
searchCheck = tk.Checkbutton(modulesMain, text = "Check", variable = moduleChecks[1])
searchCheck.grid()
saveButton = tk.Button(modulesMain, text = "Save Changes", command = SaveChanges)
saveButton.grid()
modulesMain.mainloop()
Here is a basic solution.
import tkinter as tk
root = tk.Tk()
#list of options. doubles as checkbox label text
#appending to this list is all that is necessary to create more options
opts = ['App', 'Search']
#create an StringVar for every option
#this way there doesn't have to be type conversions for reading/writing files
vars = [tk.StringVar(value='0') for _ in opts]
#create checkboxes for every option
for opt, var in zip(opts, vars):
tk.Checkbutton(root, text=opt, variable=var, onvalue='1', offvalue='0').grid(sticky='w')
#write the checkbox values to file
def save_options_state():
with open("modules.txt", "w") as file:
file.write(''.join([v.get() for v in vars]))
#set checkbox values from file
def load_options_state():
with open("modules.txt", "r") as file:
for n, var in zip(list(file.read().strip()), vars):
var.set(n)
#save/load buttons
tk.Button(root, text="save", command=save_options_state).grid()
tk.Button(root, text="load", command=load_options_state).grid(row=2, column=1)
root.mainloop()
Here's a runnable example which I think shows how to do everything you've asked about. It assumes the modules.txt file contains data in the following format, where each line consists of a module's name and whether it's checked or not.
Mod1,0
Mod2,0
Mod3,0
The data in the file is used to create a list of Checkbuttons — one for each module — and initially sets to its current status as indicated in the file.
Here's the sample code:
from tkinter import *
import tkinter.messagebox as box
dataFilename = "modules.txt"
moduleChecks = []
def ReadModules():
with open(dataFilename, "r") as file:
for line in (line.strip() for line in file):
moduleName, moduleState = line.split(',')
moduleChecks.append((moduleName, int(moduleState)))
print('read:', moduleChecks)
def SaveChanges():
print('writing:', moduleChecks)
with open(dataFilename, "w") as file:
for checkButton in moduleCheckbuttons:
moduleName = checkButton.cget('text')
moduleState = checkButton.var.get()
file.write(','.join((moduleName, str(moduleState))) + '\n')
ReadModules()
modulesMain = Tk()
moduleCheckbuttons = []
# Create a Checkbutton for each module based on moduleChecks values.
for moduleName, moduleState in moduleChecks:
intVar = IntVar(value=moduleState)
checkButton = Checkbutton(modulesMain, text=moduleName, variable=intVar)
checkButton.var = intVar # Attach reference to variable.
checkButton.grid()
moduleCheckbuttons.append(checkButton)
saveButton = Button(modulesMain, text="Save Changes", command=SaveChanges)
saveButton.grid()
modulesMain.mainloop()
Hi Tkinter and Python Masters,
Here's my problem. I'm creating a simple GUI with Tkinter. I have a background image that I would like to change based on the value of a variable defined in a function that connects to an API (JSON). The variable will hold one of two values. Either a 1 or a 2.
What I would like to do in Tkinter (which I'm totally lost on) is have a background image. If the value of the variable in my API function is 1 show Image1 as the background. If the value of the variable is 2, show Image2 as the background.
It is a bit messy, but here's my API function
def apiconnect(statusvar):
def to_serializable(ticketid):
return str(ticketid)
url = "http://staging2.apiname.com/ticket_api/tickets"
data = {'ticket_id' : ticketid, 'direction' : 'up'}
headers = {'Content-Type' : 'application/json', 'Authorization' : 'J0XxxxxVRy9hMF9Fo7j5'}
r = requests.post(url, data=json.dumps(data), headers=headers)
requestpost = requests.post(url, headers = headers, json = data)
response_data = requestpost.json()
statusvar = (response_data["status"])
messagevar = (response_data["message"])
json.dumps(url,data)
global accessResult
if statusvar == "successful":
accessResult = 1
else:
accessResult = 2
Is this possible in Tkinter? I basically want to reference my variable accessResult in my Tkinter frame, and change the background image based on that value.
Be gentle with me, I'm all new and such
So I'm no expert with Tkinter but if you want to change the background based on only two values why not use a boolean instead so:
# Here is where we make the check.
if accessResult == True:
background_photo = PhotoImage(file = "C:\\Path\\To\\My\First\\\Photo1.png")
else:
background_photo = PhotoImage(file = "C:\\Path\\To\\My\\Second\\Photo2.png")
backgroundLabel = Label(top, image=backgroundPhoto)
backgroundLabel.place(x=0, y=0, relwidth=1, relheight=1)
Good luck! Hope this helps!
I have a bunch of images which I want the user to select using a scale. As the user updates the scale value, I want the GUI to update which image is shown.
I have just started dealing with GUIs and I'm stuck. I've managed to print the new values from the scale using the command keyword argument of the Scale widget. However, it is not clear how I can get this value to update the image on the interface.
class MainProgram():
def AcquireDicomFiles(self):
# HERE I GET THE IMAGES PATHS. IT WORKS FINE.
def GUI(self):
root_window = Tk()
slice_number = DoubleVar()
bar_length = 200
main_title = Label(root_window, text="Seleção de corte e echo").pack()
scale_slice = Scale(root_window, variable=slice_number, orient=HORIZONTAL, from_=1, to=24, length=bar_length,
cursor="hand", label="Slice Number", command=MainProgram().get_slice_value)
scale_slice.pack(anchor=CENTER)
echo_time = DoubleVar()
scale_echo = Scale(root_window, variable=echo_time, orient=HORIZONTAL, from_=1, to=6, length=bar_length,
cursor="hand", label="Echo Time")
scale_echo.pack(anchor=CENTER)
imagefile = Image.open("image.png")
tk_image = ImageTk.PhotoImage(imagefile)
panel = Label(root_window, image=tk_image).pack(fill="both", expand="yes")
root_window.mainloop()
def get_slice_number(self,user_slice_number):
user_slice_number = np.int(user_slice_number)
def showImage(self):
# Code below works fine for user input in terminal. It uses user_slice_number and user_echo_time to find image. I want these two values to come from the pair of scales
# indexes = np.where(slice_and_echo[:][0] == user_slice_number)[0] #indexes of the elements where the user input match the element
# for i in indexes: #Go through index values. Check and record those in which echo time (element) matches user input
# if slice_and_echo[i][1] == user_echo_time:
# selected_echo_time = user_echo_time
# selected_slice_number = slice_and_echo[i][0]
# index = i
# file_path = os.path.join(dcm_folder, dcm_files[index]) #path of the file whose index match user input
# dcm_read = dicom.read_file(file_path) #read file user wants
# dcm_pixel_values = dcm_read.pixel_array #extract pixel values
slice_and_echo = MainProgram().AcquireDicomFiles()
MainProgram().GUI()
Set echo_time.trace(slider_callback), (which calls the method given to it, whenever the echo_time changes value) and then within slider_callback, you set root_window.iconify(file_name).
This might be a strange question because I am new to Python.
I am trying to create form in Python which data can be entered into boxes and saved, then opened again. I'm currently using Tkinter to create a Gui which has entry boxes and buttons:
import sys
from tkinter import *
def mstore():
pass
return
def msearch():
file_path = filedialog.askopenfilename()
return
mGui=Tk()
mGui.geometry('450x450+200+200')
mGui.title('Form Test')
#Top
mTitle = Label (mGui,text='Heading Text',bg='white').grid(row=1,column=1)
mDetail = Label (mGui,text='Flavour you can see',bg='white').grid(row=2,column=1)
#Entry Boxes
mFName = Label (mGui,text='Barcode',bg='white').grid(row=3,column=1)
mEntryname = Entry().grid(row=3,column=2)
#Buttons
mSave = Button (mGui,text='Save',bg='white', command = mstore).grid(row=4,column=1)
mSearch = Button (mGui,text='Search',bg='white', command = msearch).grid(row=5,column=1)
mGui.mainloop()
The search was going to be used to open up a file which has been saved before and fill in the boxes with that data, however before that I need help saving the data in a way it will be retrievable - All the information I find is about web-forms. I have also tried saving information with SQLite3 but I found that to not be quite what I was looking for.
Any help/guidance will be appreciated.
Thanks,
Hello Gregulimy!
I have simplified your code and made it do what you want it to do. I have left comments explaining what the code does. If you have any questions about what I have done feel free to ask!
from tkinter import *
def mstore(text):
file = open("file.txt", "w") # Create file.txt
file.write(text) # Write contents of mEntryname to file
file.close() # Closes text file
def msearch():
file = filedialog.askopenfilename() # Stores file directory that user chose
open_file = open(file, 'r') # Opens file user chose
print(open_file.read()) # Displays contents in console
open_file.close() # Closes text file
# Window Creation and Settings
window = Tk()
window.geometry('450x500')
window.title('Form Test')
# Create Widgets
mTitle = Label (window,text='Heading Text',bg='white')
mDetail = Label (window,text='Flavour you can see',bg='white')
mFName = Label (window,text='Barcode',bg='white')
mEntryname = Entry(window)
# Runs mstore function when pressed (passing the contents of the entry box)
mSave = Button (window,text='Save',bg='white', command = lambda: mstore(mEntryname.get()))
# Runs msearch function when pressed
mSearch = Button (window,text='Search',bg='white', command = lambda: msearch())
# Render Widgets
mTitle.pack()
mDetail.pack()
mFName.pack()
mEntryname.pack()
mSave.pack()
mSearch.pack()
window.mainloop()