如何将(我打印的任何内容)插入文本框(TInter)?试试,但它挂了?请求帮助

当我不习惯向文本框插入(我打印的任何内容)时,它将不会插入
它只是打印,然后挂起来

选择 | 换行 | 行号
  1. # a look at the Tkinter Text widget
  2.  
  3. # use ctrl+c to copy, ctrl+x to cut selected text,
  4.  
  5. # ctrl+v to paste, and ctrl+/ to select all
  6.   # count words in a text and show the first ten items
  7. # by decreasing frequency
  8.  
  9. import Tkinter as tk
  10. import os, glob
  11. import sys
  12. import string
  13. import re
  14. import tkFileDialog      
  15. def most_frequant_word():    
  16. browser= tkFileDialog.askdirectory()
  17. #browser= os.listdir(a)
  18.  
  19.  
  20. for root, dirs, files in os.walk(browser):
  21.     print 'Looking into %s' % root.split('\\')[-1]
  22.     print 'Found %d dirs and %d files' % (len(dirs), len(files))
  23.     #text1.insert(tk.INSERT,'Looking into %s' % root.split('\\')[-1])
  24.     #text1.insert(tk.INSERT, 'Found %d dirs and %d files' % (len(dirs), len(files)))
  25.     for idx, file in enumerate(files):
  26.      print 'File #%d: %s' % (idx + 1, file)
  27.       #text1.insert(tk.INSERT, 'File #%d: %s' % (idx + 1, file))
  28.      ff = open (os.path.join(root, file), "r")
  29.      text = ff.read ( )
  30.      ff.close ( )
  31.      word_freq = {}
  32.  
  33.      word_list = text.strip().split()
  34.  
  35.      for word in word_list:
  36.       word = word.lower().rstrip('.,/"-_;\\[]()')
  37.  
  38.       if word.isalpha():
  39.                 # build the dictionary
  40.        count = word_freq.get(word, 0)
  41.        word_freq[word] = count + 1
  42.  
  43.        # create a list of (freq, word) tuples
  44.        freq_list = [(freq, word) for word, freq in word_freq.items()]
  45.  
  46.        # sort the list by the first element in each tuple (default)
  47.        freq_list.sort(reverse=True)
  48.  
  49.      for n, tup in enumerate(freq_list):
  50.     # print the first ten items
  51.       if n < 50:
  52.         print "%s times: %s" % tup
  53.         text1.insert(tk.INSERT, freq)
  54.         text1.insert(tk.INSERT, word)
  55.         text1.insert(tk.INSERT, "\n")
  56.  
  57. raw_input('\nHit enter to exit')
  58.  
  59. root = tk.Tk(className = " most_frequant_word")
  60. # text entry field, width=width chars, height=lines text
  61. v1 = tk.StringVar()
  62. text1 = tk.Text(root, width=50, height=20, bg='green')
  63. text1.pack()
  64. # function listed in command will be executed on button click
  65. button1 = tk.Button(root, text='Brows', command=most_frequant_word)
  66. button1.pack(pady=5)
  67. text1.focus()
  68. root.mainloop()

尝试插入到文本框中的代码

选择 | 换行 | 行号
  1. print "%s times: %s" % tup
  2.         text1.insert(tk.INSERT, freq)
  3.         text1.insert(tk.INSERT, word)
  4.         text1.insert(tk.INSERT, "\n")

当我不习惯在文本框中插入文件名和目录时,它也会挂起
代码为注释

选择 | 换行 | 行号
  1. print 'Looking into %s' % root.split('\\')[-1]
  2.     print 'Found %d dirs and %d files' % (len(dirs), len(files))
  3.     #text1.insert(tk.INSERT,'Looking into %s' % root.split('\\')[-1])
  4.     #text1.insert(tk.INSERT, 'Found %d dirs and %d files' % (len(dirs), len(files)))
  5.     for idx, file in enumerate(files):
  6.      print 'File #%d: %s' % (idx + 1, file)
  7.       #text1.insert(tk.INSERT, 'File #%d: %s' % (idx + 1, file))
# 回答1


请停止用同一问题的多个帖子向论坛发送垃圾邮件.如果你能进一步澄清你的问题,这也会非常有帮助,因为它们并不总是很容易理解.
这里的问题是,您没有修改我们为您提供的代码,使其不能用于您自己的目的.您只需复制和粘贴,而无需费心了解代码在做什么.
我已经修复了你发布的代码,但我不会费心发布它.在找出明显的语法错误之后,我所要做的就是删除RAW_INPUT("按Enter键退出")行.这让我相信你没有花时间弄清楚这条线有什么用处.
此外,"常客"和"眉毛"应经常浏览.
# 回答2


首先,非常感谢您的帮助
其次,我真的很抱歉让你生气了
第三,我保证我不会再这样做了

标签: python

添加新评论