Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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_Python 2.7_Tkinter - Fatal编程技术网

Python 在Tkinter画布小部件中键入文本

Python 在Tkinter画布小部件中键入文本,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我需要允许用户在画布小部件中键入文本,在用户键入新文本时使画布更新 这是我到目前为止尝试过的,但我无法让它发挥作用 首先,我有一个mouseDown方法,它绑定到Button-1事件 widget.bind(self.canvas, "<Button-1>", self.mouseDown) 我在canvas小部件上还有一个全局绑定,用于捕获任何按键,如下所示: Widget.bind(self.canvas, "<Any KeyPress>", self.curren

我需要允许用户在画布小部件中键入文本,在用户键入新文本时使画布更新

这是我到目前为止尝试过的,但我无法让它发挥作用

首先,我有一个
mouseDown
方法,它绑定到Button-1事件

widget.bind(self.canvas, "<Button-1>", self.mouseDown)
我在canvas小部件上还有一个全局绑定,用于捕获任何按键,如下所示:

Widget.bind(self.canvas, "<Any KeyPress>", self.currentTypedText)

def currentTypedText(self, event):
    self.typedtext = str(event.keysym)
    self.drawText(self, self.startx, self.starty,self.foreground)
Widget.bind(self.canvas,“,self.currentTypedText)
def currentTypedText(自身,事件):
self.typedtext=str(event.keysym)
self.drawText(self,self.startx,self.starty,self.foreground)

但是没有错误,画布上也不会打印任何内容。

您想做的事情非常复杂,需要大量代码才能正常工作。您将需要处理单击事件、按键事件、特殊按键事件(如“Shift”和“Ctrl”)、“Backspace”和删除事件等

然而,第一个是第一个,这是让文本在用户输入时出现在画布中。现在,因为我没有你的完整剧本,所以我不能按原样处理你的东西。然而,我去做了我自己的小应用程序,完全符合你的要求。希望它能为我们的未来带来曙光:

from Tkinter import *

class App(Tk):

    def __init__(self):
        Tk.__init__(self)
        # self.x and self.y are the current mouse position
        # They are set to None here because nobody has clicked anywhere yet.
        self.x = None
        self.y = None
        self.makeCanvas()
        self.bind("<Any KeyPress>", lambda event: self.drawText(event.keysym))

    def makeCanvas(self):
        self.canvas = Canvas(self)
        self.canvas.pack()
        self.canvas.bind("<Button-1>", self.mouseDown)

    def mouseDown(self, event):
        # Set self.x and self.y to the current mouse position
        self.x = event.x
        self.y = event.y

    def drawText(self, newkey):
        # The if statement makes sure we have clicked somewhere.
        if None not in {self.x, self.y}:
            self.canvas.create_text(self.x, self.y, text=newkey)
            # I set x to increase by 5 each time (it looked the nicest).
            # 4 smashed the letters and 6 left gaps.
            self.x += 5

App().mainloop()
从Tkinter导入*
类应用程序(Tk):
定义初始化(自):
Tk.\uuuuuu初始(自我)
#self.x和self.y是当前鼠标位置
#因为还没有人单击过任何地方,所以此处将它们设置为“无”。
self.x=无
self.y=None
self.makeCanvas()
self.bind(“,lambda事件:self.drawText(event.keysym))
def makeCanvas(自我):
self.canvas=画布(self)
self.canvas.pack()
self.canvas.bind(“,self.mouseDown)
def鼠标向下移动(自身、事件):
#将self.x和self.y设置为当前鼠标位置
self.x=event.x
self.y=event.y
def drawText(self,newkey):
#if语句确保我们单击了某个地方。
如果{self.x,self.y}中没有:
self.canvas.create_文本(self.x,self.y,text=newkey)
#我将x设置为每次增加5(看起来最好)。
#4个打碎了字母,6个留下了缺口。
self.x+=5
App().mainloop()

单击画布中的某个位置并开始键入后,您将看到文本出现。但是请注意,我没有启用此功能来处理文本的删除(这有点棘手,超出了您的问题范围)。

我意识到这个答案已经有几年了。无论如何,我想指出一个事实,您不必为每次击键都创建一个新的文本项。画布文本项是可编辑的。有关实现,请参见
from Tkinter import *

class App(Tk):

    def __init__(self):
        Tk.__init__(self)
        # self.x and self.y are the current mouse position
        # They are set to None here because nobody has clicked anywhere yet.
        self.x = None
        self.y = None
        self.makeCanvas()
        self.bind("<Any KeyPress>", lambda event: self.drawText(event.keysym))

    def makeCanvas(self):
        self.canvas = Canvas(self)
        self.canvas.pack()
        self.canvas.bind("<Button-1>", self.mouseDown)

    def mouseDown(self, event):
        # Set self.x and self.y to the current mouse position
        self.x = event.x
        self.y = event.y

    def drawText(self, newkey):
        # The if statement makes sure we have clicked somewhere.
        if None not in {self.x, self.y}:
            self.canvas.create_text(self.x, self.y, text=newkey)
            # I set x to increase by 5 each time (it looked the nicest).
            # 4 smashed the letters and 6 left gaps.
            self.x += 5

App().mainloop()