0
from tkinter import *
def setup():
    master = Tk()
    string_converter_main = StringVar()
    entry1 = Entry(master, width = "13", textvariable = string_converter_main, font = ("Helvetica", 23), bg="gray13", fg = "ghostwhite", bd="10")
    entry1.grid(row=1, column=0, columnspan=5, sticky=W+E+S+N)

setup()

This is a bit of my code which i am using for my python tkinter calculator. It is to long to put everything but the problem i am facing is you can type a 0 first in the entry and then put a normal number following it. For example i can type 0 then a 5 and it would say 05 in my entry. How can i make this delete the 0 and replace it unless if it is followed by a decimal point or if there is a number in front of it already. Also how to stop multiple zeroes being typed into entry as well.

  • Do you have any particular reason to rewrite the input field's contents as the user types, rather than just processing the entered value internally after the user enters it? Rewriting a user's input as they type can be really annoying. – user2357112 May 09 '18 at 21:04
  • Have you seen this answer, which shows how to use the built-in input validation features of the entry widget? http://stackoverflow.com/a/4140988/7432 – Bryan Oakley May 09 '18 at 23:23

1 Answers1

0

I would check the field with an if/else statement.

Update:

Here is my next attempt based off of your comments. I am not 100% sure it will work perfectly but from my testing it seams to do what you want.

I have created some conditional if/else statements that will check to see if a specific combination of characters exist in the string. If so then reformat to remove the zero.

Take a look at the below code.

import tkinter as tk
from tkinter import messagebox

master = tk.Tk()

string_converter_main = tk.StringVar()
entry1 = tk.Entry(master, width = "33", textvariable = string_converter_main, font = ("Helvetica", 23), bg="gray13", fg = "ghostwhite", bd="10")
entry1.grid(row=1, column=0, columnspan=5, sticky="nsew")


def check_format_of_string(event):
    if event.keysym not in ["BackSpace", "Control_L", "Control_R", "Delete"] and "0" in entry1.get():
        word_list = entry1.get().split(" ")
        reformated_word_list = []
        final_string = ""
        for word_section in word_list:
            if word_section[:2] in ["01", "02", "03", "04", "05", "06", "07", "08", "09"]:
                reformated_word_list.append(word_section[1:])
            elif word_section == "00":
                reformated_word_list.append("")
            else:
                reformated_word_list.append(word_section)

        for item in reformated_word_list:
            cs = "{} {}".format(final_string, item)
            if cs[-3:] in ["0 ", " 0 "]:
                cs = cs[:-2]
            if cs[-2:] in ["  "]:
                cs = cs[:-1]

            final_string = cs

        entry1.delete(0, "end")
        entry1.insert(0, final_string.lstrip())

entry1.bind("<Key>",check_format_of_string)

master.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Is there a way to make it replace the 0 if the number is not follwed by a 0 instead of bringing a message box and then deleting it? – John Smith May 09 '18 at 21:41
  • @JohnSmith sure see my 2nd example. – Mike - SMT May 09 '18 at 22:02
  • thank you thats what i was looking for but can you make it update instantly for example when typing it into the entry it updates without having to finish the equation and press enter? – John Smith May 09 '18 at 22:05
  • Probably but I am not at my desk any more. I will check when I get home – Mike - SMT May 09 '18 at 22:08
  • brother? are you still there – John Smith May 11 '18 at 01:40
  • @JohnSmith ya but I keep running into problems with my attempts. I have some code that will remove the zero if it is by itself but its not tracking properly with the `.` or other numbers. I am sure there is a better way then what I am trying to do. – Mike - SMT May 11 '18 at 02:17
  • @JohnSmith I have added a new example to my answer. Let me know if this is what you want. I think it will work for your needs. You can likely combine it with normal validation to prevent the use of letters as well. – Mike - SMT May 11 '18 at 14:18
  • a guy in my class has it so that when he types into his entry the 0 is replaced instantly without the enter key being pressed or if it is not followed by a decimal point the other instances how is this? – John Smith May 15 '18 at 08:28
  • @JohnSmith that is the same thing my 3rd example is doing. – Mike - SMT May 15 '18 at 12:04