简体   繁体   English

为什么即使保留参考,我的画布图像也会消失?

[英]Why does my canvas image disappear even though I keep a reference?

The idea is to have the user click on the color bar and the entry field on the left will get a number from 0 to 100 entered for the user. 想法是让用户单击颜色栏,左侧的输入字段将为用户输入一个从0到100的数字。 The problem is the color bar image is blanked, even though I try to keep a reference to it so that it doesn't get garbage collected. 问题是,即使我尝试保留对它的引用以使它不会被垃圾收集,但彩条图像还是空白的。

color_bar_blue_to_red_020.GIF

import Tkinter as Tk

class Test(Tk.Frame):
    '''Test color bar for user input'''

    def __init__(self, parent):
        Tk.Frame.__init__(self, parent)

        photo_color_bar_01 = Tk.PhotoImage(file = 'color_bar_blue_to_red_020.GIF')
        pic_width = photo_color_bar_01.width()
        pic_height = photo_color_bar_01.height()
        shift_x_01 = 0 # 10
        shift_y_01 = 0 # 10
        pic_width_plus_border = pic_width + shift_x_01
        pic_height_plus_border = pic_height + shift_y_01
        x_center = (pic_width_plus_border) / 2.0
        y_center = (pic_height_plus_border) / 2.0

        My_Labels = ["Input_One", "Input_Two", "Input_Three", "Input_Four"]
        Labels = []
        variables = []
        entrys = []
        pic_frames = []
        canvases = []
        label_row = 0
        entry_row = 1
        for index in range(len(My_Labels)):

            Labels.append(Tk.Label(root, text=My_Labels[index]))
            Labels[-1].grid(padx=0, pady=0, row=label_row, column=0)

            variables.append(Tk.StringVar())
            entrys.append(Tk.Entry(root, textvariable =variables[index]))
            entrys[-1].grid(padx=0, pady=0, row=entry_row, column=0)
            entrys[-1].delete(0, Tk.END)
            entrys[-1].insert(0, "50.00")

            pic_frames.append(Tk.Frame(root, bd=4, relief=Tk.RAISED))
            pic_frames[-1].grid(padx=0, pady=0, row=entry_row, column=2)

            canvases.append(Tk.Canvas(pic_frames[-1], width=pic_width, height=pic_height))
            canvases[-1].create_image(x_center, y_center, image=photo_color_bar_01, anchor = Tk.CENTER)
            # keep a reference
            # http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm
            canvases[-1].image = photo_color_bar_01 # keep a reference
            canvases[-1].config(cursor="X_cursor")
            canvases[-1].grid(padx=0, pady=0, row=entry_row, column=3)
            canvases[-1].bind("<ButtonPress-1>", lambda event, arg=index: self.pick_LMB(event, index))
            canvases[-1].bind("<ButtonPress-2>", self.pick_MMB)

            label_row += 2
            entry_row += 2

    def pick_LMB(self, event, index):
        print "\nindex = " + str(index)
        canvas = event.widget
        x = canvas.canvasx(event.x)
        width_x = 180.0
        scaled_x = x/width_x * 100.0
        print "scaled_x = " + '{0:.2f}'.format(scaled_x)
        #entrys[index].delete(0, Tk.END)
        #entrys[index].insert(0, '{0:.2f}'.format(scaled_x))

    def pick_MMB(self, event):
        canvas = event.widget
        x = canvas.canvasx(event.x)
        print "\nx = " + str(x)
        width_x = 180.0
        scaled_x = x/width_x * 100.0
        print "scaled_x = " + '{0:.2f}'.format(scaled_x)

if __name__ == "__main__":
    root = Tk.Tk()
    Test(root)
    root.mainloop()

我忘记了.create_image命令,因此引用毕竟不是问题。

暂无
暂无

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

相关问题 当我不按按钮时,为什么我的图像消失了? - Why does my image disappear when I'm not pressing a button? 为什么即使我有预定义的答案,这仍然会返回“对不起” - Why does this keep returning 'sorry' even though i have a predefined answer 为什么即使设置了 while 条件,代码仍会继续递归? - Why does the code keep recursing even though a condition for while is set? 为什么在输入文本字段后我的画布消失了? - Why does my canvas disappear after making a text field entry? 即使元素确实存在,我仍然在 selenium 中收到 NoSuchElementException - I keep getting a NoSuchElementException in selenium even though the element DOES exist 为什么即使输入是整数,我也会不断收到 TypeError? - Why do I keep getting TypeError even though input is an interger? 为什么我的 Pygame 标签不显示,即使我从另一个有效的标签中复制? - Why does my Pygame label not show up even though I copied from my other one that worked? 为什么我的Flask应用未配置为重定向一条路由? - Why does my Flask app redirect one route even though I haven't configured it to? 为什么即使我外包给工作线程,我的GUI也没有响应? - Why does my GUI go unresponsive even though I'm outsourcing to a worker thread? 为什么即使我在 VS Code 中更改了解释器,我的 python 版本仍然显示版本 2.7? - Why does my python version still show version 2.7 even though I changed the interpreter in VS Code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM