像素大小Tkinter文本小部件 - 文本截断

时间:2016-03-28 16:06:47

标签: python text tkinter

我是tkinter的初学者,非常感谢以下代码的帮助。我无法理解为什么左侧框架中的文本被截断 - 这似乎暗示框架比显示的框架大。如何将其包裹在显示的区域内?

from Tkinter import *
import tkFont

class Display:

    def __init__(self, master):
        app = Frame(master)
        app.grid()

        self.l_frame = Frame(app,width=200, height=500)
        self.r_frame = Frame(app,width=500, height=500)
        self.r_frame.grid_propagate(False)
        self.l_frame.grid_propagate(False)
        self.l_frame.grid(row=0, column=0, sticky=W)
        self.r_frame.grid(row=0, column=1, sticky=E)

        self.mono_font = tkFont.Font(family="monospace",size=24,weight="bold")
        self.instructions = "A longish string of text which I want to display using Tkinter but for some reason gets truncated"

        self.info = Text(self.l_frame, wrap=WORD, padx=10, pady=10, bd=5)
        self.info.insert(1.0,self.instructions)
        self.info.grid()

        self.island = Text(self.r_frame, bg="cyan", padx=20, pady=20, font=self.mono_font)
        self.island.insert(1.0, "ready")
        self.island.grid()

root = Tk()
game = Display(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

我对tkinter中的.grid方法不太熟悉,但我设法做了我认为你正在寻找使用Label选项和一些.grid定位的东西。

from Tkinter import *
import tkFont

class Display:

    def __init__(self, master):
        app = Frame(master)
        app.grid()

        self.l_frame = Frame(app,width=200, height=500)
        self.r_frame = Frame(app,width=500, height=500)
        self.r_frame.grid_propagate(False)
        self.l_frame.grid_propagate(False)
        self.l_frame.grid(row=0, column=0, sticky=W)
        self.r_frame.grid(row=0, column=1, sticky=E)

        self.mono_font = tkFont.Font(family="monospace",size=24,weight="bold")
        self.instructions = Label(root, wraplength=195, text="A longish string of text which I want to display using Tkinter but for some reason gets truncated") # Made this a label with wraplength

        self.info = Text(self.l_frame, wrap=WORD, padx=10, pady=10, bd=5)
        #self.info.insert(1.0,self.instructions) #Commented this out, didn't seem to need it.
        self.instructions.grid(row=0, sticky=NW)

        self.island = Text(self.r_frame, bg="cyan", padx=20, pady=20, font=self.mono_font)
        self.island.insert(1.0, "ready")
        self.island.grid()

root = Tk()
game = Display(root)
root.mainloop()