gpt4 book ai didi

Python Tkinter : Delete the Last Character of a String

转载 作者:太空狗 更新时间:2023-10-30 01:03:07 25 4
gpt4 key购买 nike

我正在创建一个只允许输入数字的条目。如果该字符不是整数,我目前坚持删除刚刚输入的字符。如果有人将用需要进入的内容替换“空白”,那将有很大帮助。

import Tkinter as tk

class Test(tk.Tk):

def __init__(self):

tk.Tk.__init__(self)

self.e = tk.Entry(self)
self.e.pack()
self.e.bind("<KeyRelease>", self.on_KeyRelease)

tk.mainloop()



def on_KeyRelease(self, event):

#Check to see if string consists of only integers
if self.e.get().isdigit() == False:

self.e.delete("BLANK", 'end')#I need to replace 0 with the last character of the string

else:
#print the string of integers
print self.e.get()




test = Test()

最佳答案

您也可以将上面的行更改为:

    if not self.e.get().isdigit():
#take the string currently in the widget, all the way up to the last character
txt = self.e.get()[:-1]
#clear the widget of text
self.e.delete(0, tk.END)
#insert the new string, sans the last character
self.e.insert(0, txt)

或:

if not self.e.get().isdigit():
#get the length of the string in the widget, and subtract one, and delete everything up to the end
self.e.delete(len(self.e.get)-1, tk.END)

很好地提供了一个可供我们使用的工作示例,有助于加快这一进程。

关于Python Tkinter : Delete the Last Character of a String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com