我正在爲基於文本的遊戲設計一個真正基本的佈局。爲什麼第一列中的文本在字符串結尾之前停止,爲什麼窗口如此之高?代碼如下,任何幫助表示讚賞。Tkinter帶文本的基本佈局
from Tkinter import *
import tkFont
class TreasureHunt:
def __init__(self, master):
app = Frame(master)
app.grid()
self.mono_font = tkFont.Font(family="monospace",size=24,weight="bold")
self.instructions = "Find the hidden treasure!\n\nUse the arrow keys to select where to look, then press Enter to check. \
There is a 50/50 chance you will be told the distance from the treasure. Keep hunting until you find it. Good luck!"
self.info = Text(app, wrap=WORD, padx=10, pady=10, bd=0,width=10)
self.info.insert(1.0,self.instructions)
self.info.grid(row=0,column=0,sticky=N)
self.island = Text(app, bg="cyan",bd=0, padx=20, pady=20, font=self.mono_font,width=20)
self.island.insert(1.0, "ready")
self.island.grid(row=0,column=1)
self.display_grid()
def display_grid(self):
self.matrix = [["# " for col in range(8)] for row in range(8)]
for row in range(len(self.matrix)):
row_str = "".join(self.matrix[row]) +"\n"
self.island.insert(1.0,row_str)
root = Tk()
game = TreasureHunt(root)
root.mainloop()
我不明白這部分問題:「爲什麼第一列中的文本在字符串結尾之前停止」 。它似乎沒有「停止」給我 - 它包裹到下一行。你是這個意思嗎? –
隨着代碼原樣,文本停在「來自寶藏」。 「繼續狩獵直到你找到它,祝你好運!」不見了。我猜測第一列的垂直空間已滿,儘管由於右列的垂直高度而出現其他情況。 – Robin