John Keith
John Keith

Reputation: 93

I can't seem to figure out how to update tkinter labels

I'm trying to write a script to accept an image, then process the image and put a grid over it. I haven't merged the script that modifies the image yet. I'm trying to put a front end on this, I'm planning on publishing the script in a DnD facebook group for others to use to overlay grids onto their battlemaps. I can't seem to have the GUI update the labels which display the pixel length of the image the user selects.

    import tkinter as tk
    from tkinter import filedialog
    import imageGrid
    import sys
    from PIL import *
    from PIL import Image





    root= tk.Tk()
    root.withdraw()
    iWidth = tk.StringVar()
    iHeight = tk.StringVar()
    class pinger(tk.Tk):


def __init__(self, parent):
    tk.Tk.__init__(self, parent)
    self.parent = parent
    self.initialize()
    

def initialize(self):        
    self.grid()
    button = tk.Button(self,text="exit",command=lambda: closeProgram())
    button.grid(column=3,row=9)
    buttonOpen = tk.Button(self, text="Select an Image", command= lambda: openExplorer()
                           )
    buttonOpen.grid(column=2, row=2)
    labelSig = tk.Label(self, text='By Johnathan Keith, 2020. Ver 1.0')
    labelSig.grid(column=3,row=10)
    labelImgWidth = tk.Label(self, textvariable=iWidth)
    labelImgWidth.grid(column=2,row=3)
    labelStaticImg= tk.Label(self, text="Width of image, in pixels: ")
    labelStaticImg.grid(column=1,row=3)
    labelStaticHeight= tk.Label(self, text="Height of image, in pixels: ")
    labelStaticHeight.grid(column=3,row=3)
    labelImgHeight = tk.Label(self, textvariable=iHeight)
    labelImgHeight.grid(column=4,row=3)
    labelWidth = tk.Label(self, text='Enter the width of the grid, in pixels.')
    labelWidth.grid(column=4,row=2)
    labelDisclaim = tk.Label(self, text='Currently only works with jpegs')
    labelDisclaim.grid(column=2, row=1)

def openFile(imagefilename):
    Img = Image.open(imagefilename)
    height, width = Img.size
    iHeight.set(height)
    iWidth.set(width)
def closeProgram():
    app.destroy()
    sys.exit()
def openExplorer():
    app.filename= filedialog.askopenfilename(initialdir="/", title="Select an Image", filetypes=(("jpeg files", "*.jpg"),("all files", "*.*")))
    if app.filename:
       print(app.filename)
       pinger.openFile(app.filename)



if __name__ == "__main__":
    app = pinger(None)
    app.title('Image Gridder')
    app.minsize(height=680,width=480)
    app.mainloop()

I've been searching through other SE questions and none of them seemed to work with the way my code is written. I'm trying to update the StringVar()'s iWidth and iHeight which will eventually allow the user to specify how they want the grid to overlay the image. I've tried moving them all over the code, in and out of the class, and nothing works. Also, StackExchange kinda butchered the indentation, so don't mind that.

Thank you!

Upvotes: 0

Views: 40

Answers (1)

acw1668
acw1668

Reputation: 46687

It is because you have two instances of Tk(): root and app (pinger). StringVar iWidth and iHeight are within root scope, other widgets are within app scope. So the content of the StringVars are not shown in widgets within app.

You can remove root stuff and only have app as the only instance of Tk():

import tkinter as tk
from tkinter import filedialog
from PIL import Image

class pinger(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.initialize()

    def initialize(self):        
        self.iWidth = tk.StringVar()
        self.iHeight = tk.StringVar()

        # row 1
        labelDisclaim = tk.Label(self, text='Currently only works with jpegs')
        labelDisclaim.grid(column=2, row=1)

        # row 2
        labelWidth = tk.Label(self, text='Enter the width of the grid, in pixels.')
        labelWidth.grid(column=4,row=2)

        buttonOpen = tk.Button(self, text="Select an Image", command=self.openExplorer)
        buttonOpen.grid(column=2, row=2)

        # row 3
        labelStaticImg= tk.Label(self, text="Width of image, in pixels: ")
        labelStaticImg.grid(column=1,row=3)

        labelImgWidth = tk.Label(self, textvariable=self.iWidth)
        labelImgWidth.grid(column=2,row=3)

        labelStaticHeight= tk.Label(self, text="Height of image, in pixels: ")
        labelStaticHeight.grid(column=3,row=3)

        labelImgHeight = tk.Label(self, textvariable=self.iHeight)
        labelImgHeight.grid(column=4,row=3)

        # row 9
        button = tk.Button(self,text="exit",command=self.closeProgram)
        button.grid(column=3,row=9)

        # row 10
        labelSig = tk.Label(self, text='By Johnathan Keith, 2020. Ver 1.0')
        labelSig.grid(column=3,row=10)

    def openFile(self, imagefilename):
        Img = Image.open(imagefilename)
        height, width = Img.size
        self.iHeight.set(height)
        self.iWidth.set(width)

    def closeProgram(self):
        self.destroy()

    def openExplorer(self):
        filename= filedialog.askopenfilename(initialdir="/", title="Select an Image", filetypes=(("jpeg files", "*.jpg"),("all files", "*.*")))
        if filename:
           print(filename)
           self.openFile(filename)

if __name__ == "__main__":
    app = pinger()
    app.title('Image Gridder')
    app.minsize(height=680,width=480)
    app.mainloop()

Upvotes: 1

Related Questions