简体   繁体   中英

Why does this character ▯ appear?

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 is my full code.

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()

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.

So, you should change this fragment:

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

to this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM