简体   繁体   English

我怎样才能在 tkinter 的文本中得到文字

[英]How can i get the writing in a text in tkinter

I have a Text widget that contains two rows of text.我有一个包含两行文本的文本小部件。 I need to get the row when i click on it.当我单击它时,我需要获取该行。 For example if i click on the second row of text ı should get the only second row of text.例如,如果我单击第二行文本,我应该得到唯一的第二行文本。 I tried to bind it but it didnt work.我试图绑定它,但它没有工作。

And also there is an another problem.而且还有另一个问题。 Bind function only works for one time.绑定 function 只能使用一次。 If i click on the Text widget second time it does not work.如果我第二次单击文本小部件,它将不起作用。 My code is getting every row of text not that i clicked on.我的代码正在获取不是我点击的每一行文本。

from tkinter import *
import time

root = Tk()

clicked = StringVar()


text = Text(root,height = 10, width = 30,relief = SUNKEN )
text.grid(row = 0, column = 0,padx = 20,pady = 20)
text.insert(END,"Oda the Elder\nJohn the Baptist")

def getelement(event):
    for t in range(1,3):
        get_ = text.get(f"{t}.0",f"{t+1}.0")
        print(get_)

text.bind("<FocusIn>",getelement)

but = Button(root,text = "click")
but.grid(row = 1, column = 0)


root.mainloop()

You can bind to the button click event ( <1> ).您可以绑定到按钮单击事件( <1> )。 Within the function you can get the index based on the x/y of the mouse at the time of the click.在 function 中,您可以根据单击时鼠标的 x/y 获得索引。 From that you can get the text of that whole line by modifying the index with "linestart" and "lineend".从中可以通过使用“linestart”和“lineend”修改索引来获取整行的文本。

The function would look something like this: function 看起来像这样:

def getelement(event):
    index = event.widget.index(f"@{event.x},{event.y}")
    text = event.widget.get(f"{index} linestart", f"{index} lineend")
    print(text)

The binding would look like this:绑定看起来像这样:

text.bind("<1>",getelement)

You can't do this with the Text box as everything is inside that text box only.您不能对文本框执行此操作,因为所有内容都仅在该文本框内。 The lines are theoretically defined.这些线是理论上定义的。 You want to use a Listbox您想使用列表框

Syntax句法

Here is the simple syntax to create this widget −这是创建此小部件的简单语法 -

w = Listbox ( master, option, ... )
Parameters
master − This represents the parent window.

options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.

Below is an example of code:下面是一个代码示例:

from tkinter import *

top = Tk()

Lb1 = Listbox(top)
Lb1.insert(1, "Python")
Lb1.insert(2, "Perl")
Lb1.insert(3, "C")
Lb1.insert(4, "PHP")
Lb1.insert(5, "JSP")
Lb1.insert(6, "Ruby")
def items_selected(event):
    """ handle item selected event
    """
    # get selected indices
    selected_indices = Lb1.curselection()
    # get selected items
    selected_langs = ",".join([Lb1.get(i) for i in selected_indices])
    msg = f'You selected: {selected_langs}'

    print(msg)


Lb1.bind('<<ListboxSelect>>', items_selected)
Lb1.pack()
top.mainloop()


More details更多细节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM