Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python/Tk程序中禁用选择文本?_Python_User Interface_Text_Tkinter - Fatal编程技术网

如何在Python/Tk程序中禁用选择文本?

如何在Python/Tk程序中禁用选择文本?,python,user-interface,text,tkinter,Python,User Interface,Text,Tkinter,那怎么办? 我一直无法在这里或谷歌上找到它 #Refrences from tkinter import * class Interface: def __init__(self,stage): topLeft = Frame(stage,bg='black') test = Entry(topLeft,bg='#2a2a2a',fg='white',insertontime=0) test.config(insertbackgrou

那怎么办? 我一直无法在这里或谷歌上找到它

#Refrences
from tkinter import *

class Interface:

    def __init__(self,stage):

        topLeft = Frame(stage,bg='black')
        test = Entry(topLeft,bg='#2a2a2a',fg='white',insertontime=0)
        test.config(insertbackground='white', exportselection=0)
        test.grid()
        topLeft.grid(row=0,column=0)

def launch():
    window = Tk()
    lobby = Interface(window)
    window.mainloop()

launch()

Tkinter没有大量内置小部件(比如JavaFX)的一个有用的解决方案是,如果您不介意它们不是您想要的,您可以轻松创建自己的小部件:下面是一个粗略的示例,使用画布模拟无法选择的输入字段。我所给的只是插入功能和删除功能,我想,如果你很聪明的话,但是你可能想要更多。另一个优点是,因为它是一个画布项,所以可以使用它进行漂亮的格式化

我说的“不可选择的输入”小部件是指一个写有文本的画布,对吗?如果要在所有小部件(包括顶层框架)中禁用文本突出显示,可以突出显示Button-1事件,取消选择所有内容,然后在选择文本时使用该事件

from tkinter import *

class NewEntry(Canvas):

    def __init__( self, master, width, height ):
        Canvas.__init__( self, master, width=width,height=height )
        self.width = width
        self.height = height
        self.text = ''

    def insert( self, index, text ):
        self.text =''.join([self.text[i] for i in range(index)] + [text] + [self.text[i] for i in range(index,len(self.text))])
        self.delete(ALL)
        self.create_text(self.width//2,self.height//2,text=self.text)

root = Tk()
entry = NewEntry(root,100,100)
entry.insert(0,'hello world')
entry.insert(5,'world')
entry.pack()
root.mainloop()

我假设您希望用户不能编辑输入框?如果是这样,您必须使用state=disabled的配置参数,例如

test.config(insertbackground='white', exportselection=0, state="disabled")

不过要小心,我找不到一种方法来保持你输入框的背景为黑色。希望这有助于

当我们选择文本时,我们触发3个事件,即按钮按下、运动和按钮释放。所有这3个事件都称为事件处理程序函数。 函数run select_rangestart,end method突出显示所选文本

要禁用此功能,我们必须同时处理ButtonPress和Motion事件,并在小部件上调用select_clear方法

如果您处理事件并调用select\u clear方法,该方法工作正常但不正常,则文本将高亮显示,并且当另一个事件发生时,高亮颜色将被清除。 这是因为事件的执行顺序。这意味着您必须告诉tk在默认事件处理程序之后处理您的事件。我们可以使用bindtags和bin_类方法更改事件的执行顺序

例如:


您可以设置文本高亮显示颜色以匹配条目小部件的背景。请注意,小部件中的文本仍然可以被选择,但用户将看不到它,这将给人一种选择被禁用的错觉

test = Entry(topLeft,bg='#2a2a2a',fg='white',insertontime=0)
test.configure(selectbackground=test.cget('bg'), inactiveselectbackground=test.cget('bg'))

出于好奇,为什么?您正在删除大多数用户希望在输入小部件中找到的功能。这正是我所需要的。
test = Entry(topLeft,bg='#2a2a2a',fg='white',insertontime=0)
test.configure(selectbackground=test.cget('bg'), inactiveselectbackground=test.cget('bg'))