删除(0,END)由于某种原因不起作用。它没有给出任何错误消息,其余代码似乎没问题。删除(0,END)不会删除我在intvar的输入框中输入的0,我无法弄清楚它为什么不能工作。我在另一个代码中使用了删除,它在那里工作。谁能帮助我?
我遇到问题的代码。
part1_entry = tk.Entry(Frame1, font=('Helvetica', 12, 'bold'),
textvariable=self.a3, validate = 'key', validatecommand = vcmd, width = 11)
part1_entry.grid(column=0, row=6, sticky=(W), columnspan=1)
part1_entry.delete(0, END)
part2_entry = tk.Entry(Frame1, font=('Helvetica', 12, 'bold'),
textvariable=self.a5, validate = 'key', validatecommand = vcmd, width = 11)
part2_entry.grid(column=0, row=6, sticky=(E), columnspan=1)
part2_entry.delete(0, END)
这是我的代码。
import tkinter as tk
from tkinter import ttk
import io
from tkinter import filedialog
E=tk.E
W=tk.W
N=tk.N
S=tk.S
VERTICAL=tk.VERTICAL
END=tk.END
class Demo1(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master = master
self.Frame = tk.Frame(self.master, borderwidth=10)
self.Frame.grid(column=0, row=0, sticky=(N, W, E, S))
button1 = tk.Button(self.Frame, text="Special Rk märkning", command=self.new_window, width = 25)
button1.grid(column=0, row=0, sticky=(W, E), columnspan=4)
button2 = tk.Button(self.Frame, text="Billerud kabelmärkningar", command=self.new_window, width = 25)
button2.grid(column=0, row=1, sticky=(W, E), columnspan=4)
def new_window(self):
Billerud(self)
class Billerud:
def __init__(self, master):
self.master = master
top1 = tk.Toplevel()
top1.title("Billerud")
top1.resizable(width=False, height=False)
#fönster designern här bästems fönster ramar, rader och columner
Frame1 = tk.Frame(top1)
Frame1.grid(column=1, row=0, sticky=(N, W, E, S), padx=5, pady=5)
Frame1.columnconfigure(0, weight=1)
Frame1.rowconfigure(0, weight=1)
#Koden är en del av entry's endast nummer restriktion
vcmd = (Frame1.register(self.validate),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
#create text editor
self.text_entry = tk.Text(Frame1, width=25,height=20)
self.text_entry.grid(column=0, row=0, sticky=(W, E))
scrollbar = tk.Scrollbar(Frame1, orient=VERTICAL)
scrollbar.grid(column=0, row=0, sticky=(N, E, S))
# koppla ihop listbox med scrollbar
self.text_entry.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=self.text_entry.yview)
self.a1 = tk.StringVar()
self.a2 = tk.StringVar()
self.a3 = tk.IntVar()
self.a4 = tk.StringVar()
self.a5 = tk.IntVar()
text1 = tk.Label(Frame1, text="Kabelnamn:", font=("Helvetica", 12, "bold")).grid(column=0, row=1, sticky=(W), columnspan=2)
Kabelnamn_entry = tk.Entry(Frame1, font=('Helvetica', 12, 'bold'), textvariable=self.a1)
Kabelnamn_entry.grid(column=0, row=2, sticky=(W, E), columnspan=2)
text2 = tk.Label(Frame1, text="Kabelnummer:", font=("Helvetica", 12, "bold")).grid(column=0, row=3, sticky=(W), columnspan=2)
Kabelnummer_entry = tk.Entry(Frame1, font=('Helvetica', 12, 'bold'), textvariable=self.a2)
Kabelnummer_entry.grid(column=0, row=4, sticky=(W, E), columnspan=2)
text3 = tk.Label(Frame1, text="Parter från: Till:", font=("Helvetica", 12, "bold")).grid(column=0, row=5, sticky=(W), columnspan=1)
part1_entry = tk.Entry(Frame1, font=('Helvetica', 12, 'bold'), textvariable=self.a3, validate = 'key', validatecommand = vcmd, width = 11)
part1_entry.grid(column=0, row=6, sticky=(W), columnspan=1)
part1_entry.delete(0, END)
part2_entry = tk.Entry(Frame1, font=('Helvetica', 12, 'bold'), textvariable=self.a5, validate = 'key', validatecommand = vcmd, width = 11)
part2_entry.grid(column=0, row=6, sticky=(E), columnspan=1)
part2_entry.delete(0, END)
button1 = tk.Button(Frame1, text="Make", command=self.funktion, width = 16)
button1.grid(column=0, row=7, sticky=(W, E), columnspan=4)
button2 = tk.Button(Frame1, text="Spara", command=self.file_save, width = 16)
button2.grid(column=0, row=8, sticky=(W, E), columnspan=4)
top1.update()
top1.resizable(width=False, height=False)
top1.mainloop()
def validate(self, action, index, value_if_allowed,
prior_value, text, validation_type, trigger_type, widget_name):
if text in '0123456789.-+':
try:
float(value_if_allowed)
return True
except ValueError:
return False
else:
return False
def file_save(self):
file = tk.filedialog.asksaveasfile(defaultextension=".txt", mode='wt', filetypes = (("txt files","*.txt"),("all files","*.*")))
if file:
data = self.text_entry.get('1.0', END+'-1c')
file.write(data)
file.close()
def funktion(self):
value1 = (self.a1.get())
value2 = (self.a2.get())
value3 = (self.a3.get())
value4 = (self.a4.get())
value5 = (self.a5.get())
for parts in range(value3-1, value5):
print('{}-{}-{}-{}\n'.format(value1, value2, parts+1, parts+1))
self.text_entry.insert(END, '{}-{}-{}-{}\n'.format(value1, value2, parts+1, parts+1))
self.a4.set('{}-{}-{}-{}\n'.format(value1, value2, parts+1, parts+1))
def close_windows(self):
self.master.destroy()
def main():
root = tk.Tk()
app = Demo1(root)
root.protocol("WM_DELETE_WINDOW")
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:0)
幸运的是,我现在有太多的空闲时间。 尝试使用:
self.a3.set("")
self.a5.set("")
也许知道tkinter的人可以提供更多帮助。
答案 1 :(得分:0)
我认为你应该尝试:
part1_entry.delete(0, 'end')
part2_entry.delete(0, 'end')