为什么会显示此字符▯? [英] Why does this character ▯ appear?

查看:82
本文介绍了为什么会显示此字符▯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,当我运行代码时会出现此字符which,我认为这意味着缺少字符,因此无法显示。 (如果我错了,请不要纠正我)基本上,我希望能够摆脱这个角色。这是我运行代码时的样子:

So this character ▯ appears when I run my code which I think means there is a missing character therefor it can't be displayed. (Not sure correct me if I am wrong) And well basically I want to be able to get rid of that character. Here is what it looks like when I run my code:

但是,当我单击其中一个框以使其在顶部显示时,在空闲的后端,它不会注册并在空闲状态下看起来像这样:

However in the back-end in the idle when I click on one of the boxes for it to be displayed up top it doesn't register and looks like this in idle:

如果它不空闲时为什么会出现在屏幕上?
另外,如何从主屏幕上删除▯字符?

Why does it appear on screen if it isn't going to appear in idle? Also how can I get rid of the ▯ character from the main screen?

这是我的完整代码。

以下是我认为问题所在的部分。 (但是我无法解决问题)

Here are segments in which I think the problem lies. (However I have not been able to solve the problem)

我的树比较类可以找到句子及其频繁使用:

My classes for Tree comparison to find the sentences and their frequent use:

class Branch():
    def __init__(self, value):
        self.left = None
        self.right = None
        self.value = value
        self.frequency = 1

    def incFreq(self):
        self.frequency = self.frequency + 1

    def freq(self):
        return self.frequency

class Tree():    

    highest = []

    def __init__(self):
        self.root = None
        self.found = False        

    def findHighest(self):
        from operator import itemgetter, attrgetter
        self.highest = []
        self.inorder(self.root)
        self.highest = sorted(self.highest, key=itemgetter(1), reverse=True)        
        return self.highest

    #lessThan function needed to compare strings
    def lessThan(self, a, b):    
        if len(a) < len(b):
            loopCount = len(a)
        else:
            loopCount = len(b)        
        for pos in range(0, loopCount):
            if a[pos] > b[pos]:
                return False        
        return True

    def outputTree(self):
        self.inorder(self.root)

    def insert(self, value):
        #increment freq if already exists, else insert
        if not self.exists(value):            
            self.root = self.insertAtBranch(self.root, value)

    def exists(self, value):
        #set the class variable found to False to assume it is not there      
        self.found = False
        self.findAtBranch(self.root, value)
        return self.found

    #Used to fine a value in a tree
    def findAtBranch(self, branch, value):        
        if branch == None:
            pass
        else:
            #print ("[" + branch.value + "][" + value + "]") # Error checking
            if branch.value == value:
                self.found = True
                #print("found " + value)
                branch.incFreq()
                #print(branch.freq())
            else:
                self.findAtBranch(branch.left, value)
                self.findAtBranch(branch.right, value)        

    def insertAtBranch(self, branch, value):
        if branch == None:
            return Branch(value)
        else:
            if self.lessThan(branch.value, value):
                branch.right = self.insertAtBranch(branch.right, value)            
            else:
                branch.left = self.insertAtBranch(branch.left, value)
            return branch

    def inorder(self, branch):
        if branch == None: return
        self.highest.append((branch.value, branch.freq()))
        #print (branch.value)
        #print (branch.freq())
        #print(self.highest[0])
        self.inorder(branch.left)
        self.inorder(branch.right)

这是我在此处使用树并传递要在其他函数上使用的句子的地方:

This is where I use the tree and pass sentences to be used on a different function:

def getPhrases(self, numToReturn):

    topPhrases = []
    phrasesTree = Tree()

    #load tree with phrases from phrase text file
    file = open('setPhrases.txt', 'r')
    for line in file:    
        phrasesTree.insert(line)

    #create a list of the top n of phrases to return
    val = 0
    for phrase in phrasesTree.findHighest():
        if val < numToReturn:
            topPhrases.append(phrase)
        val = val + 1

    return topPhrases

在这里我使用这些句子在屏幕上显示它们:

This is where I use the sentences to be able to display them on the screen:

def createPhrases(self):

    print("createPhrases")
    self.deletePanes()

    self.show_keyboard = False
    self.show_words = False
    self.show_phrases = True
    self.show_terminal = True

    words = self.getPhrases(10)
    for word, count in words:
        self.addPane("{}".format(word, count), WORDS)
    self.addPane("Boxes", PHRASE)
    self.addPane("Keyboard", PHRASE)
    self.addPane("OK", PHRASE)
    self.drawPanes()


推荐答案

从文件中读取行时,换行符在末尾。 pygame的文档指出:

When you read lines from file, newline characters are at the end. pygame's documentation states that:


文本只能是一行:不显示换行符。

The text can only be a single line: newline characters are not rendered.

您应该更改此片段:

file = open('setPhrases.txt', 'r')
for line in file:    
    phrasesTree.insert(line)

为此:

file = open('setPhrases.txt', 'r')
for line in file:    
    phrasesTree.insert(line.strip())

这篇关于为什么会显示此字符▯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆