2015-12-28 72 views
3

我正在編寫一個輕量級webtexting應用程序,並且我試圖顯示TKinter中用於寫入消息的文本小部件中的當前字符數以webtext發送。我有此刻的代碼在下面可以看到,我使用python如何獲取Tkinter Text小部件中文本的當前長度

root = Tk() 

msgLabel = Label(text="Message") 
msgLabel.grid(row=0, column=0) 
msg = Text(width=40, height=4, wrap="word") 
msg.grid(row=0, column=1, padx=10, pady=5) 

#Try to display number of characters within message to user 
charCount = Label(text="Character Count: "+str(len(meg.get("1.0", 'end-1c'))) 
charCount.grid(row=1, column=1, pady=5, padx=5) 

root.mainloop() 

我想能夠顯示的字符數由用戶編寫的消息中,因爲有一個160字符數限制爲每個webtext。是否可以顯示當前的字符長度,並在插入和刪除文本時進行更新?在此先感謝

回答

4

我們使用StringVar來設置新的文本標籤。 我們需要綁定msg進行按鍵。

from Tkinter import * 
root = Tk() 
#----------------------------------------------- 
def update(event): 
    var.set(str(len(msg.get("1.0", 'end-1c')))) 
#----------------------------------------------- 

msgLabel = Label(text="Message") 
msgLabel.grid(row=0, column=0) 
msg = Text(width=40, height=4, wrap="word") 
msg.grid(row=0, column=1, padx=10, pady=5) 

#---------------- 
var = StringVar() 
#---------------- 

#Try to display number of characters within message to user 
#---------------------------------- 
charCount = Label(textvariable=var) 
#---------------------------------- 
charCount.grid(row=1, column=1, pady=5, padx=5) 

#------------------------ 
msg.bind("<Key>", update) 
#------------------------ 

root.mainloop() 
0

在以下實例中,在text字符計數是顯示在以上的label在任何時候。

try: 
    import tkinter as tk 
except: 
    import Tkinter as tk 


def on_every_keyboard_input(event): 
    update_char_length(text, label) 


def update_char_length(text_widget, display_widget): 
    string_in_text = text_widget.get('1.0', 'end-1c') 
    string_length = len(string_in_text) 
    display_widget['text'] = string_length 


if __name__ == '__main__': 
    root = tk.Tk() 
    text = tk.Text(root) 
    label = tk.Label(root, text=0, justify='center') 

    text.bind('<KeyPress>', on_every_keyboard_input) 
    text.bind('<KeyRelease>', on_every_keyboard_input) 

    label.pack() 
    text.pack() 
    root.mainloop() 
0

下面是一個小例子,其中在text字符計數是在所有時間顯示在label上方。

try: 
    import tkinter as tk 
except: 
    import Tkinter as tk 


if __name__ == '__main__': 
    def update(event): 
     label['text'] = len(text.get('1.0', 'end-1c')) 


    root = tk.Tk() 
    text = tk.Text(root) 
    label = tk.Label(root, text=0, justify='center') 

    text.bind('<KeyPress>', update) 
    text.bind('<KeyRelease>', update) 

    label.pack() 
    text.pack() 
    root.mainloop() 
0

下面腳本定義了一個類,TextWithCharLength,其具有Text並且其中顯示在text的內容的字符長度Label

try: 
    import tkinter as tk 
except: 
    import Tkinter as tk 


class TextWithCharLength(tk.Frame): 
    def __init__(self, master, *args, **kwargs): 
     tk.Frame.__init__(self, master, *args, **kwargs) 
     self._events = ('<KeyPress>', 
         '<KeyRelease>', 
         '<ButtonRelease-1>', 
         '<ButtonRelease-2>', 
         '<ButtonRelease-3>', 
         '<Delete>', 
         '<<Cut>>', 
         '<<Paste>>', 
         '<<Undo>>', 
         '<<Redo>>') 
     self._create_widgets() 
     self._widgets_layout() 
     self.update_char_len(event=None) 
     self.bind_events_on_widget_to_callback(self._events, 
               self.text, 
               self.update_char_len) 

    def _create_widgets(self): 
     self.label = tk.Label(self, justify='center') 
     self.text = tk.Text(self) 


    def _widgets_layout(self): 
     self.label.pack(fill='x', expand=True) 
     self.text.pack(fill='both', expand=True) 


    @staticmethod 
    def bind_events_on_widget_to_callback(events, widget, callback): 
     """ Bind events on widget to callback. 
     """ 

     for _event in events: 
      widget.bind(_event, callback) 


    def update_char_len(self, event): 
     """ Update self.label's text with character length of content 
     in self.text. 
     """ 

     self.label['text'] = len(self.text.get('1.0', 'end-1c')) 


if __name__ == '__main__': 
    root = tk.Tk() 
    text_frame = TextWithCharLength(root) 
    text_frame.pack() 
    root.mainloop() 
相關問題