I am writing an interface which consists of a label with an image, an entrybox as well as a stringvar, and a button.
I am trying to create these in a loop as there are 6 of them, to try to write more efficient code.
here's what I have:
x = 0
y = 2
for mapfile in ["pillars","cosmic","double","underpass","utopia","octagon"]:
self.widgets[mapfile+"-image"] = ImageTk.PhotoImage(Image.open("./images/png/"+mapfile+".png"))
self.widgets[mapfile+"-label"] = ttk.Label(self, image=self.widgets[mapfile+"-image"])
self. Widgets[mapfile+"-string"]= tk.StringVar()
self.widgets[mapfile+"-entry"] = ttk.Entry(self, textvariable=self.widgets[mapfile+"-string"])
self.widgets[mapfile+"-button"]= ttk.Button(self, text="Browse", padding=3, command=lambda: self.browseFiles(self.widgets[mapfile+"-string"]))
self.widgets[mapfile+"-label"].grid(column=x, row=y, columnspan=2)
self.widgets[mapfile+"-entry"].grid(column=x, row=y+1, sticky="we", padx=3, pady=0)
self.widgets[mapfile+"-button"].grid(column=x+1, row=y+1, sticky="we")
x += 2
if x > 4:
x = 0
y += 2
You can see here, I create all the elements needed for each "map" and grid them. note in the stringvar, and the button, the command I am calling a lambda function to pass a reference to the stringvar to the browseFiles method. Now, in the browseFiles method, here it is:
def browseFiles(self, mapfile):
print(str(mapfile))
filename = fd.askopenfilename( initialdir=mapfile.get(), \
title="Select Map", \
filetypes=(("Map Files", "*.upk *.udk"),
("ZIP Files", "*.zip")))
if filename != "":
mapfile.set(filename)
return filename
(the comments are there bc its not working)
within that code there, the only stringvar that is getting passed is the final one created in the loop (in this case, no matter which button I click, it prints "PYVAR_5". With the code uncommented, it only pulls (and updates) the octagon entry.
...why?
Here's the full script so you can try it out:
#!/usr/bin/env python
import os
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog as fd
from tkinter import messagebox as mb
import shutil as sh
import zipfile as zip
from PIL import ImageTk, Image
# import win32api as win
class Merl(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self)
self.root = root
self.root.title("Map Editor for Rocket League")
self.grid(column=0,row=0,sticky=("nsew"))
self.widgets = {}
# for element in ["image","label","button","entry","stringVar","command"]:
x = 0
y = 2
for mapfile in ["pillars","cosmic","double","underpass","utopia","octagon"]:
self.widgets[mapfile+"-image"] = ImageTk.PhotoImage(Image.open("./images/png/"+mapfile+".png"))
self.widgets[mapfile+"-label"] = ttk.Label(self, image=self.widgets[mapfile+"-image"])
self.widgets[mapfile+"-string"]= tk.StringVar()
self.widgets[mapfile+"-entry"] = ttk.Entry(self, textvariable=self.widgets[mapfile+"-string"])
self.widgets[mapfile+"-button"]= ttk.Button(self, text="Browse", padding=3, command=lambda: self.browseFiles(self.widgets[mapfile+"-string"]))
self.widgets[mapfile+"-label"].grid(column=x, row=y, columnspan=2)
self.widgets[mapfile+"-entry"].grid(column=x, row=y+1, sticky="we", padx=3, pady=0)
self.widgets[mapfile+"-button"].grid(column=x+1, row=y+1, sticky="we")
x += 2
if x > 4:
x = 0
y += 2
self.widgets["cosmic-string"].set("/home/betty/test.upk")
print(self.widgets)
print(self.widgets["cosmic-string"].get())
for button in ["pillars","cosmic","double","underpass","utopia","octagon"]:
print(self.widgets[button+'-button'].cget("command"))
def browseFiles(self, mapfile):
print(str(mapfile))
filename = fd.askopenfilename( initialdir=mapfile.get(), \
title="Select Map", \
filetypes=(("Map Files", "*.upk *.udk"),
("ZIP Files", "*.zip")))
if filename != "":
mapfile.set(filename)
return filename
root = tk.Tk()
merl = Merl(root)
root.mainloop()