I have used tkinter to organize pictures with text inside various frames on a canvas with a scrollbar. (as can be seen in the picture). I'm really struggling to achieve same functionalities with pysimplegui. I need help on creating a layout using Image(),Text() and Frame() Elements. I have also tried to manipulate the layout with Column Element without success. Looping through the arrangement really stops my progress.
-
Tell us more, it looks like that there's nothing about your project and your problem.Jason Yang– Jason Yang2024-12-29 16:19:20 +00:00Commented Dec 29, 2024 at 16:19
-
I'm unsure whether the picture was included or notKjell– Kjell2024-12-29 17:51:12 +00:00Commented Dec 29, 2024 at 17:51
Add a comment
|
1 Answer
To avoid the location issue for image and text, I build a Image Column for it.
If you need to work dynamically with adding elements, Image, Frame, remember to call window.refresh() and scrollable_column.contents_changed() after it, so it will update all content to keep the scroller work correctly.
Example Code
import PySimpleGUI as sg
class Image(sg.Column):
def __init__(self, source, text):
layout = [[sg.Image(source)], [sg.Text(text, size=8, justification="center")]]
super().__init__(layout, element_justification="center")
sg.theme("DarkBlue")
sg.set_options(font=("Courier New", 11))
emoji = {"Happy":sg.EMOJI_BASE64_HAPPY_LIST, "SADLY":sg.EMOJI_BASE64_SAD_LIST}
columns = 10
column_layout = []
for title, lst in emoji.items():
frame_layout, line = [], []
limit = len(lst) - 1
for i, item in enumerate(lst):
line.append(Image(item, f"{title} {i+1}"))
if i % columns == columns - 1 or i == limit:
frame_layout.append(line)
line = []
column_layout.append([sg.Frame(title, frame_layout)])
layout = [[sg.Column(column_layout, scrollable=True, vertical_scroll_only=True)]]
sg.Window("Scrollable Frames with Images", layout).read(close=True)
6 Comments
Kjell
The emoji dictionary has two keys and values given in two lists with 36 and 29 elements respectively. Can the dictionary be built using loops, say keys in some range and list with various length? The list should preferably be nested such that values in row 1 belongs to key 1 or in general row number x in the nested loop belongs to key number x
Jason Yang
Of course, you can build the nested figure lists and key list. for example:
keys = ["Happy", "SAD"]; lists = [sg.EMOJI_BASE64_HAPPY_LIST, sg.EMOJI_BASE64_SAD_LIST] and loop them by for title, lst in zip(keys, lists):.Kjell
I manage to create a dictionary in this way:
Kjell
my_dict = {serie_names[i]: img_file[i] for i in range(len(serie_names))} It should have worked days ago, but due to a bug in file name which i
Jason Yang
or
my_dict = {name:files for name, files in zip(serie_names, img_file)} |
