【问题标题】:Make a new window open below the blinking cursor using Tkinter使用 Tkinter 在闪烁的光标下方打开一个新窗口
【发布时间】:2016-03-18 04:18:24
【问题描述】:

通常我们会得到坐标并在那里打开一个新窗口。我需要的是在 Tkinter 文本框的闪烁光标下方打开一个窗口。我不知道如何获取屏幕中高度和宽度像素的坐标。

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    您需要获取 Text 小部件相对于屏幕的当前位置,以及小部件插入光标的边界框,它是相对于小部件的。

    如果 Text 小部件的元素当前不可见,则其边界框为 None;在这种情况下,您需要使用 .see 方法滚动文本以使元素可见。

    这个程序是为 Python 2 编写的,所以如果你在 Python 3 上运行它,你需要将 import 语句更改为 import tkinter as tk

    #!/usr/bin/env python
    
    ''' Text Location Demo
        Open a Tkinter window just under the location of the insertion cursor 
        of a Text widget.
    
        See http://stackoverflow.com/q/34237313/4014959
    
        Written by PM 2Ring 2015.12.12
    '''
    
    import Tkinter as tk
    
    #Some random text to display in the Text widget
    lorem_ipsum = '''Lorem ipsum dolor sit amet, consectetur adipiscing
    elit. Aenean lacinia tortor quis quam vehicula semper. Curabitur
    faucibus, purus a egestas bibendum, velit metus hendrerit nulla, non
    lobortis dolor mi in dolor. Aliquam ultrices felis sit amet dolor
    gravida, id ullamcorper odio rutrum. Fusce consectetur tempor nibh, non
    dictum dolor dictum nec. In hac habitasse platea dictumst. Morbi laoreet
    consequat metus, at lacinia nisl suscipit id. Quisque vitae sodales
    velit, a lobortis nisl. Praesent varius convallis efficitur. Vivamus
    fringilla at risus nec viverra. Proin suscipit, lorem sed laoreet
    ultricies, velit massa ornare nunc, vel egestas nibh ex vitae leo.'''
    
    lorem_ipsum = lorem_ipsum.replace('\n', ' ')
    
    class TextLocationDemo(object):
        ''' Text widget cursor location demo '''
        def __init__(self):
            root = tk.Tk()
            root.title("Text Location Demo")
    
            tk.Button(root, text="Show cursor location", 
                command=self.location_cb).pack()
    
            # Create a Text widget, with word wrapping
            self.textwidget = tw = tk.Text(root, wrap=tk.WORD)
            tw.pack()
            tw.insert(tk.END, lorem_ipsum)
    
            root.mainloop()
    
        def alert(self, geometry, msg):
            ''' Display `msg` in an Alert with given geometry,
                which is a tuple of (width, height, ox, oy)
            '''
            top = tk.Toplevel()
            # widget geometry parameter must be given in X windows format
            top.geometry("%dx%d%+d%+d" % geometry)
    
            msg = tk.Message(top, text=msg, width=geometry[0])
            msg.pack()
    
            button = tk.Button(top, text="Ok", command=top.destroy)
            button.pack()
    
        def location_cb(self):
            ''' Determine the location of the insertion cursor
                and display it in a window just under that location
            '''
            w = self.textwidget
    
            # Get the Text widget's current location
            pos_x, pos_y = w.winfo_rootx(), w.winfo_rooty()
    
            # Get the bounding box of the insertion cursor
            cursor = tk.INSERT
            bbox = w.bbox(cursor)
            if bbox is None:
                print('Cursor is not currently visible. Scrolling...')
                w.see(cursor)
                bbox = w.bbox(cursor)
    
            bb_x, bb_y, bb_w, bb_h = bbox
    
            #Open a window just beneath the insertion cursor
            width = 200
            height = 80
            ox = pos_x + bb_x
            oy = pos_y + bb_y + bb_h
            s = 'Cursor: (%d, %d)' % (ox, oy)
            print(s)
    
            geometry = (width, height, ox, oy)
            self.alert(geometry, s)
    
    
    TextLocationDemo()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 2022-11-10
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多