简体   繁体   English

我似乎无法弄清楚如何更新 tkinter 标签

[英]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.我正在尝试为此设置前端,我计划在 DnD facebook 组中发布脚本,以供其他人用来将网格叠加到他们的战斗地图上。 I can't seem to have the GUI update the labels which display the pixel length of the image the user selects.我似乎无法让 GUI 更新显示用户选择的图像的像素长度的标签。

    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.我一直在搜索其他 SE 问题,但它们似乎都不适用于我的代码编写方式。 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.我正在尝试更新 StringVar() 的 iWidth 和 iHeight,这最终将允许用户指定他们希望网格如何覆盖图像。 I've tried moving them all over the code, in and out of the class, and nothing works.我已经尝试将它们移动到整个代码中,进出 class,但没有任何效果。 Also, StackExchange kinda butchered the indentation, so don't mind that.此外,StackExchange 有点破坏了缩进,所以不要介意。

Thank you!谢谢!

It is because you have two instances of Tk() : root and app ( pinger ).这是因为您有两个Tk()实例: rootapp ( pinger )。 StringVar iWidth and iHeight are within root scope, other widgets are within app scope. StringVar iWidthiHeightroot scope 内,其他小部件在app scope 内。 So the content of the StringVars are not shown in widgets within app .所以 StringVars 的内容不会显示在app内的小部件中。

You can remove root stuff and only have app as the only instance of Tk() :您可以删除root内容,并且仅将app作为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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM