如何在python tkinter中重叠窗口小部件/框架? [英] How do you overlap widgets/frames in python tkinter?

查看:973
本文介绍了如何在python tkinter中重叠窗口小部件/框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这是否有可能.我的目标是在较大的文本字段顶部的右下角有一个小白框.当此人在文本字段内滚动浏览文本时,白框将用作信息框".

I was wondering if this is even possible. My goal is to have a small white box in the bottom right hand corner, on top of a larger text field. The white box will be used as an "info box" while the person scrolls through the text inside of the text field.

当我说文本字段"时,是指tkinter中的文本.

When I say "text field" I mean Text from tkinter.

推荐答案

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

The way to place one widget on top of other widgets is to use the place geometry manager. You can specify an x/y coordinate relative to some other widget, as well as to specify either absolute or relative widths and heights.

effbot网站在场所几何图形管理器上写得不错: http://effbot.org/tkinterbook /place.htm

The effbot site has a decent writeup on the place geometry manager: 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()

这篇关于如何在python tkinter中重叠窗口小部件/框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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