Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter无法绘制到其他窗口_Python_Tkinter_Tkinter Entry - Fatal编程技术网

Python Tkinter无法绘制到其他窗口

Python Tkinter无法绘制到其他窗口,python,tkinter,tkinter-entry,Python,Tkinter,Tkinter Entry,我想做一些应该很简单的事情,但我正在努力让它工作 基本上我有两个Tkinter窗口(canvas\u tk和control\u tk) 在canvas\u tk中,我想显示一个图像并在其上画一个圆圈 在control\u tk中,我有一个条目输入要绘制的圆的半径 在我的代码中,关键行位于代码的最底部: self.panel = tk.Label(canvas_tk, image=image) 如果我在控件_-tk中创建标签,或者如果我没有指定父控件,它实际上可以正常工作,并在控件_-tk窗口中

我想做一些应该很简单的事情,但我正在努力让它工作

基本上我有两个Tkinter窗口(
canvas\u tk
control\u tk

canvas\u tk
中,我想显示一个图像并在其上画一个圆圈

control\u tk
中,我有一个条目输入要绘制的圆的半径

在我的代码中,关键行位于代码的最底部:

self.panel = tk.Label(canvas_tk, image=image)
如果我在控件_-tk中创建标签,或者如果我没有指定父控件,它实际上可以正常工作,并在控件_-tk窗口中绘制圆 但是,我希望在canvas_tk窗口中绘制圆

self.panel = tk.Label(image=image)              # Works
self.panel = tk.Label(control_tk, image=image)  # Works
self.panel = tk.Label(canvas_tk, image=image)    # Fails
以下是我的最低代码:

import tkinter as tk
from PIL import Image, ImageTk, ImageDraw

control_tk = tk.Tk()
canvas_tk = tk.Tk()
control_tk.geometry("300x300")
canvas_tk.geometry("900x900")

class Drawing:
    def __init__(self):
        # Numerical entry with a variable traced with callback to "on_change" function
        self.radius_var = tk.IntVar()
        self.radius_var.trace_variable("w", self.on_change)
        tk.Entry(control_tk, textvariable=self.radius_var).grid()

        # Initialize image and panel
        self.image = Image.new('RGB', (1000, 1000))
        self.panel = None

        # mainloop for the two tkinter windows
        control_tk.mainloop()
        canvas_tk.mainloop()

    def on_change(self, *args):
        print("Value changed")   # to check that function is being called


        draw = ImageDraw.Draw(self.image)
        draw.ellipse((50-self.radius_var.get(), 50-self.radius_var.get(),
                      50+self.radius_var.get(), 50+self.radius_var.get()),
                     outline='blue', width=3)
        image = ImageTk.PhotoImage(self.image)

        if self.panel:  # update the image
            self.panel.configure(image=image)
            self.panel.image = image
        else:   # if it's the first time initialize the panel
            self.panel = tk.Label(canvas_tk, image=image)
            self.panel.image = image
            self.panel.grid(sticky="w")

Drawing()
而回溯错误基本上是抱怨图像不存在

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\lab\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 508, in get
    return self._tk.getint(value)
_tkinter.TclError: expected integer but got ""

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\lab\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/GIT/142-277-00_pyScuti2/test.py", line 29, in on_change
    draw.ellipse((50-self.radius_var.get(), 50-self.radius_var.get(),
  File "C:\Users\lab\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 510, in get
    return int(self._tk.getdouble(value))
_tkinter.TclError: expected floating-point number but got ""
Value changed
Value changed
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\lab\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/GIT/142-277-00_pyScuti2/test.py", line 38, in on_change
    self.panel = tk.Label(canvas_tk, image=image)
  File "C:\Users\lab\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2766, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\lab\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 2299, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist

首先,您不应该使用多个
Tk()
实例。其次,最好不要在跟踪输入更改时使用
trace()
,在用户输入值并按
Enter
键后,使用
bind(“”,…)
触发绘图。以下是基于您的修改后的代码:

import tkinter as tk
from PIL import Image, ImageTk, ImageDraw

control_tk = tk.Tk()
control_tk.geometry("300x300+800+600")

canvas_tk = tk.Toplevel() # use Toplevel instead of Tk
canvas_tk.geometry("900x900+10+10")

class Drawing:
    def __init__(self):
        # Numerical entry with a variable traced with callback to "on_change" function
        self.radius_var = tk.IntVar()
        entry = tk.Entry(control_tk, textvariable=self.radius_var)
        entry.grid()
        # binding Enter key on entry to trigger the drawing
        entry.bind('<Return>', self.on_change)

        # Initialize image and panel
        self.image = Image.new('RGB', (1000, 1000))
        self.panel = None

        # mainloop for the main window
        control_tk.mainloop()

    def on_change(self, *args):
        try:
            radius = self.radius_var.get()
        except:
            print('Invalid number')
            return

        print("Value changed")   # to check that function is being called

        draw = ImageDraw.Draw(self.image)
        draw.ellipse((50-radius, 50-radius, 50+radius, 50+radius),
                     outline='blue', width=3)
        image = ImageTk.PhotoImage(self.image)

        if self.panel:
            self.panel.configure(image=image)
        else:
            self.panel = tk.Label(canvas_tk, image=image)
            self.panel.grid(sticky='w')
        self.panel.image = image

Drawing()
将tkinter作为tk导入
从PIL导入图像、ImageTk、ImageDraw
control_tk=tk.tk()
控制几何(“300x300+800+600”)
canvas_tk=tk.Toplevel()#使用Toplevel而不是tk
帆布几何(“900x900+10+10”)
课堂绘图:
定义初始化(自):
#带有变量跟踪的数值输入,并回调“on_change”函数
self.radius_var=tk.IntVar()
entry=tk.entry(control\u tk,textvariable=self.radius\u var)
entry.grid()
#在条目上绑定Enter键以触发图形
条目绑定(“”,自我更改时)
#初始化图像和面板
self.image=image.new('RGB',(10001000))
self.panel=无
#主窗口的mainloop
控制_tk.mainloop()
更改时的def(自身,*参数):
尝试:
radius=self.radius\u var.get()
除:
打印('无效编号')
返回
打印(“值已更改”)#以检查是否正在调用该函数
draw=ImageDraw.draw(self.image)
绘制椭圆((50半径,50半径,50+半径,50+半径),
轮廓(蓝色),宽度=3)
image=ImageTk.PhotoImage(self.image)
如果是self.panel:
self.panel.configure(image=image)
其他:
self.panel=tk.Label(canvas\u tk,image=image)
self.panel.grid(sticky='w')
self.panel.image=image
图纸()
读取并删除