简体   繁体   中英

Is there a config option for non-editable prefixes in entry field

I am trying to make my own console using Tkinter, and I want to be able to display a prefix in the entry field and also use console.prefix("prefix_goes_here") to set said prefix. But when I use entry.set("prefix_goes_here") the user has the ability to delete the prefix.

example of what I mean can be found in CMD

C:\Users\Nathan>command_goes_here

everything before the ">" is what I would class as the prefix (but I do not know if it has an official name so I am just clarifying).

I also would preferably like to still be able to grab this prefix using entry.get(), but I could store the prefix in a variable and just add it later on.

There is no configuration option.

One technique is to use the validation feature of an Entry widget. In the validation function, you can check that the entry contains the prefix and reject the edit if it does not.

For more example on entry validation see Interactively validating Entry widget content in tkinter

Example

import tkinter as tk

class Example():
    def __init__(self):
        root = tk.Tk()

        self.prefix = r'C:\Users\Nathan> '
        vcmd = (root.register(self.onValidate), '%P')
        self.entry = tk.Entry(root, validate="key", validatecommand=vcmd)
        self.entry.pack(padx=20, pady=20)
        self.entry.insert(0, self.prefix)

    def onValidate(self, P):
        return P.startswith(self.prefix)

e = Example()
tk.mainloop()

Although i didnt find exactly what you are asking, i would suggest using 2 entry widgets one next to the other without borders.

import tkinter as tk
root = tk.Tk()
e = tk.Entry(root)
e.configure(state="normal",borderwidth=0,highlightthickness=0)
e.insert(tk.END, "C:\\Users\\Nathan>")
e.configure(bg='white')
e.place(x=0,y=0)
e2 = tk.Entry(root)
e2.configure(bg='white',borderwidth=0,highlightthickness=0)
e2.place(x=97,y=0)
e2.config(state="normal")
root.mainloop()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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