我会用 if/else 语句检查该字段。
更新:
这是我根据您的评论进行的下一次尝试。我不是 100% 确定它会完美地工作,但从我的测试来看,它接缝可以做你想做的事。
我创建了一些条件 if/else 语句,它们将检查字符串中是否存在特定的字符组合。如果是这样,则重新格式化以删除零。
看看下面的代码。
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()