1

I made a GUI where I have four entry field and I have to keep keypad symbol dot(.) fixed at cursor position 3,7 and 11.So whenever user entered any value symbol dot(.) have to be there at their fixed position regardless of entered input value.But in my case I got a dot symbol at starting three position and also that dot symbol are changing its position so anyone can help me to solve this problem in my below given code.

from tkinter import *
root = Tk()
e1=Entry(root, width=15, background='white',
                                   font='-weight bold')
e2= Entry(root, width=15, background='white',
                            font='-weight bold')
e3= Entry(root, width=15, background='white',
                                font='-weight bold')
e4= Entry(root, width=15, background='white',
                                   font='-weight bold')

e1.grid(padx=10, pady=5, row=0, column=1)
e2.grid(padx=10, pady=5, row=1, column=1)
e3.grid(padx=10, pady=5, row=2, column=1)
e4.grid(padx=10, pady=5, row=3, column=1)

entries=[e1,e2,e3,e4]

for widget in entries:
    for i in range(3,12,4):
        widget.insert(i,'.')

root.mainloop()

I have to make ENTRY Filed like this

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
ARYA1992
  • 131
  • 4
  • 16
  • If you need them at position 3, 7, and 11, what do you do if the user deletes everything, or they delete the last few characters so that there are only 9 characters? What if, for example, the user types in "123" - what do you expect to see in the widget? – Bryan Oakley Aug 14 '18 at 15:02
  • I want to see like this: 255.123.255.125 so I have to keep by default dot at fixed position so I have to keep dot always on entry field. – ARYA1992 Aug 14 '18 at 15:11
  • It's still hard to understand what you want. You've got four entry widgets of width 10. 10 isn't wide enough to show four groups of three, but it's too wide if all you need is three digits. Are you wanting each entry widget to hold xxx.xxx.xxx.xxx, or should each widget be for just three digits, and you want a dot to appear between each entry widget? – Bryan Oakley Aug 14 '18 at 15:15
  • Oh ya its my fault width of entry field is 15 character.So now I want all four entry field like this xxx.xxx.xxx.xxx where at the end of each 3 charactor there should be dot (.) in all of four entry widget. – ARYA1992 Aug 14 '18 at 15:20
  • See above I uploaded an Image which shows what exactly I want to implement. @Bryan Oakley – ARYA1992 Aug 14 '18 at 15:35
  • You can make 3 entry fields for each IP address, and display them with a label with a dot between them; or have one entry field per IP, and attached it to a label that displays the proper formatting with the dots at the right place, while the user fills in the entry. – Reblochon Masque Aug 14 '18 at 15:43
  • UI-wise how will you handle `1.1.1.1` which is a valid IP address? The dots will not be at position 3, 7 and 11 and forcing people to enter `001.001.001.001` is not very funny – Patrick Mevzek Aug 14 '18 at 15:58
  • Yes you are right in this can what should I have to do @PatrickMevzek do you have any suggestion. – ARYA1992 Aug 15 '18 at 02:33

1 Answers1

1

The simplest solution in my opinion is to create a custom widget that is made up of four Entry widgets of length 3, plus three Label widgets for the dots, all inside a Frame which is used to provide the border.

For example:

class IPEntry(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent, borderwidth=1, relief="sunken",
                          background="white")
        self.entries = []
        for i in range(4):
            entry = tk.Entry(self, width=3, borderwidth=0,
                             justify="center",
                             highlightthickness=0, background="white")
            entry.pack(side="left")
            self.entries.append(entry)

            if i < 3:
                dot = tk.Label(self, text=".", background="white")
                dot.pack(side="left")

    def get(self):
        return ".".join([entry.get() for entry in self.entries])

You can use this like you do any other widget:

root = tk.Tk()
field = IPEntry(root)
field.pack(side="top")

The above would create a window that looks like this:

screenshot

To get the value as a string, call the get method:

print("the field value is: {}".format(field.get()))

To give a more enjoyable user experience you should probably add input validation to allow only numbers, and to allow a max of three per entry widget. You could also add code to move the cursor to the next field once the user enters three digits in a field, and move the cursor to the previous field if they hit backspace at the start of an entry field.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I tried above custom widget but in that even though width of each entry field is 3 it takes more than 3 character in each entry field. – ARYA1992 Aug 14 '18 at 17:04
  • @ARYA1992: yes, you'll need to add some input validation to only allow digits, and only allow a max of three. See the accepted answer to [Interactively validating Entry widget content in tkinter](https://stackoverflow.com/questions/4140437/interactively-validating-entry-widget-content-in-tkinter) – Bryan Oakley Aug 14 '18 at 17:19