2013-12-11 52 views
2

我有以下代碼:創建一個文本框,就像在MS塗料在Python

from Tkinter import * 

def b1down(event): 
    print 'hello' 


root = Tk() 
drawing_area = Canvas(root) 
drawing_area.pack() 
drawing_area.bind("<ButtonPress-1>", b1down) 
root.mainloop() 

我想,當我點擊的地方,有一個新的文本框,允許用戶輸入這樣的一些文字(如在畫圖繪圖軟件)

enter image description here

你有一個想法,一種Python對象的使用? (我不認爲一個Tkinter的Entry此處將適應...)

回答

2

一個Entry widget是你所需要的(它可以在這裏適應)

只需將鼠標點擊的位置Entry部件:

def b1down(event): 
    entry = Entry(root,bd=0,font=("Purisa",15)) #No Border and added font:) 
    entry.place(x= event.x, y= event.y) 
    entry.focus_force() 

root = Tk() 
root.columnconfigure(0, weight=1) 
root.rowconfigure(0, weight=1) 
drawing_area = Canvas(root) 
drawing_area.grid(sticky=N+E+S+W) 
drawing_area.bind("<ButtonPress-1>", b1down) 
root.mainloop() 

爲了讓鼠標點擊的位置使用event.xevent.y方法。

編輯:

  • 答案是進一步編輯,以滿足額外要求。看到下面的評論
+0

謝謝...是的,它的工作原理,但有一個邊界(我不希望任何邊境),而且是可以改變字體和字體大小? – Basj

+0

@Basj現在檢查,考慮一個upvote,如果它幫助你... –

+0

它幫助了我很多:) – Basj

1

如果你想要一個真實的項目在畫布上 - 與嵌入式項目小部件 - 你可以使文本項目可編輯。它需要一些工作來設置綁定,但它非常簡單。

下面是一個例子:

import Tkinter as tk 

class Example(tk.Frame): 

    def __init__(self, root): 
     tk.Frame.__init__(self, root) 

     # create a canvas where we can demonstrate the technique 
     self.canvas = tk.Canvas(self) 
     self.canvas.pack(side="top", fill="both", expand=True) 

     # text items with the tag "editable" will inherit these bindings 
     self.canvas.tag_bind("editable","<Double-Button-1>", self.set_focus) 
     self.canvas.tag_bind("editable","<Button-1>", self.set_cursor) 
     self.canvas.tag_bind("editable","<Key>", self.do_key) 
     self.canvas.tag_bind("editable","<Home>", self.do_home) 
     self.canvas.tag_bind("editable","<End>", self.do_end) 
     self.canvas.tag_bind("editable","<Left>", self.do_left) 
     self.canvas.tag_bind("editable","<Right>", self.do_right) 
     self.canvas.tag_bind("editable","<BackSpace>", self.do_backspace) 
     self.canvas.tag_bind("editable","<Return>", self.do_return) 

     # create some sample text 
     self.canvas.create_text(20,20, anchor="nw", fill="blue", 
           text="double-click the black text to edit.\n" + \ 
            "Blue text cannot be edited (for\n" + \ 
            "illustrative purposes.") 
     self.canvas.create_text(20, 80, anchor="nw", tags=("editable",), 
           text="This text is editable") 
     self.canvas.create_text(20, 100, anchor="nw", tags=("editable",), 
           text="Press <Return> when done editing") 

    def do_return(self,event): 
     '''Handle the return key by turning off editing''' 

     self.canvas.focus("") 
     self.canvas.delete("highlight") 
     self.canvas.select_clear() 

    def do_left(self, event): 
     '''Move text cursor one character to the left''' 

     item = self.canvas.focus() 
     if item: 
      new_index = self.canvas.index(item, "insert") - 1 
      self.canvas.icursor(item, new_index) 
      self.canvas.select_clear() 

    def do_right(self, event): 
     '''Move text cursor one character to the right''' 

     item = self.canvas.focus() 
     if item: 
      new_index = self.canvas.index(item, "insert") + 1 
      self.canvas.icursor(item, new_index) 
      self.canvas.select_clear() 

    def do_backspace(self, event): 
     '''Handle the backspace key''' 

     item = self.canvas.focus() 
     if item: 
      selection = self.canvas.select_item() 
      if selection: 
       self.canvas.dchars(item, "sel.first", "sel.last") 
       self.canvas.select_clear() 
      else: 
       insert = self.canvas.index(item, "insert") 
       if insert > 0: 
        self.canvas.dchars(item, insert-1, insert) 
      self.highlight(item) 

    def do_home(self, event): 
     '''Move text cursor to the start of the text item''' 

     item = self.canvas.focus() 
     if item: 
      self.canvas.icursor(item, 0) 
      self.canvas.select_clear() 

    def do_end(self, event): 
     '''Move text cursor to the end of the text item''' 

     item = self.canvas.focus() 
     if item: 
      self.canvas.icursor(item, "end") 
      self.canvas.select_clear() 

    def do_key(self, event): 
     '''Handle the insertion of characters''' 

     item = self.canvas.focus() 
     if item and event.char >= " ": 
      insert = self.canvas.index(item, "insert") 
      selection = self.canvas.select_item() 
      if selection: 
       self.canvas.dchars(item, "sel.first", "sel.last") 
       self.canvas.select_clear() 
      self.canvas.insert(item, "insert", event.char) 
      self.highlight(item) 

    def highlight(self, item): 
     '''Highlight the given text item to show that it's editable''' 

     items = self.canvas.find_withtag("highlight") 
     if len(items) == 0: 
      # no highlight box; create it 
      id = self.canvas.create_rectangle((0,0,0,0), fill="white",outline="blue", 
               dash=".", tag="highlight") 
      self.canvas.lower(id, item) 
     else: 
      id = items[0] 

     # resize the highlight 
     bbox = self.canvas.bbox(item) 
     rect_bbox = (bbox[0]-4, bbox[1]-4, bbox[2]+4, bbox[3]+4) 
     self.canvas.coords(id, rect_bbox) 

    def set_focus(self, event): 
     '''Give focus to the text element under the cursor''' 

     if self.canvas.type("current") == "text": 
      self.canvas.focus_set() 
      self.canvas.focus("current") 
      self.canvas.select_from("current", 0) 
      self.canvas.select_to("current", "end") 
      self.highlight("current") 

    def set_cursor(self, event): 
     '''Move the insertion point''' 

     item = self.canvas.focus() 
     if item: 
      x = self.canvas.canvasx(event.x) 
      y = self.canvas.canvasy(event.y) 

      self.canvas.icursor(item, "@%d,%d" % (x, y)) 
      self.canvas.select_clear() 


if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop() 
+0

Waw!非常感謝,這段代碼肯定對我的項目有用! 但是,看起來如果我們使用這個類,我需要重新定義一切:複製/粘貼,剪切等,以及在文本框中發生的大量「常見」事情。你怎麼看 ? – Basj

+1

@Basj:是的,你必須實現剪切,複製,粘貼和其他一些功能。這是一點點工作,但很簡單。正如你所看到的,實現箭頭,退格,回家和結束都只是幾行代碼。這是一個典型的折衷方案,您必須決定哪個更重要:您的時間或最終用戶體驗。 –

+1

這個解決方案是令人印象深刻的+1 :) –