【问题标题】:How do you overlap widgets/frames in python tkinter?你如何在 python tkinter 中重叠小部件/框架?
【发布时间】:2013-11-25 17:52:03
【问题描述】:

我想知道这是否可能。我的目标是在右下角有一个小白框,在较大的文本字段之上。当用户滚动文本字段内的文本时,白框将用作“信息框”。

当我说“文本字段”时,我指的是来自 tkinter 的文本。

【问题讨论】:

标签: python tkinter overlay overlap ttk


【解决方案1】:

将一个小部件置于其他小部件之上的方法是使用place 几何管理器。您可以指定相对于其他小部件的 x/y 坐标,以及指定绝对或相对宽度和高度。

effbot 网站对位置几何管理器有一篇不错的文章:http://effbot.org/tkinterbook/place.htm

这是一个简单的例子:

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.text = tk.Text(self, wrap="word")
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.text.yview)
        self.text.configure(yscrollcommand=self.text_yview)
        self.vsb.pack(side="right", fill="y")
        self.text.pack(side="left", fill="both", expand=True)

        # create an info window in the bottom right corner and
        # inset a couple of pixels
        self.info = tk.Label(self.text, width=20, borderwidth=1, relief="solid")
        self.info.place(relx=1.0, rely=1.0, x=-2, y=-2,anchor="se")

    def text_yview(self, *args):
        ''' 
        This gets called whenever the yview changes.  For this example
        we'll update the label to show the line number of the first
        visible row. 
        '''
        # first, update the scrollbar to reflect the state of the widget
        self.vsb.set(*args)

        # get index of first visible line, and put that in the label
        index = self.text.index("@0,0")
        self.info.configure(text=index)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

【讨论】:

  • 谢谢,它工作得很好。不过,我还有另一个问题。我试图找出如何在 Text TKinter python 项目中找到文本光标的当前位置。文本光标表示“|”在你的写作位置旁边闪烁。
  • @user3033423:self.text.index("insert")会以line.character的形式返回插入光标的索引
猜你喜欢
  • 1970-01-01
  • 2021-05-29
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
  • 2021-01-12
  • 1970-01-01
相关资源
最近更新 更多