繁体   English   中英

tkinter中的输入窗口未删除

[英]Entry window in tkinter is not deleting

我正在处理此代码,以计算在gcode文件中找到的某些值。 在按钮gcode文件加载的体积中找到一个体积值,然后找到质量和价格。 我希望能够使用输入函数输入这些变量,但首先还要有一些默认值。 我的问题是输入窗口不会清除并接受其他值,只有那些默认值在那里。 我正在使用entry.delete(0,END),但是它不起作用。

这是代码:

    def delete_entry(self):
        e.delete(0, END)
        return None

#    def Statusbar(self):
#         self.stat1.set("Waiting for the file... ")

    #Creation of init_window
    def init_window(self):

        # changing the title of our master widget      
        self.master.title("Filament Data")

        # allowing the widget to take the full space of the root window
        self.pack(fill=BOTH, expand=1)

        # creating a menu instance
        menu = Menu(self.master)
        self.master.config(menu=menu)

        # create the file object)
        file = Menu(menu)
        help = Menu(menu)

        # adds a command to the menu option, calling it exit, and the command it runs on event is client_exit
        file.add_command(label="Exit", command=self.client_exit)
        help.add_command(label="About", command=self.about_popup)

        #added "file" to our menu
        menu.add_cascade(label="File", menu=file)
        menu.add_cascade(label="Help", menu=help)


        #Creating the  intro label
        l_instruction = Label(self, justify=CENTER, compound=TOP, text="Enter density and price per \n gram of your material and then \n load GCODE file to find volume, \n weight and price of used filament.")
        l_instruction.grid(columnspan=2, ipady=10)


        #Creating the button
        gcodeButton = Button(self, text="Load GCODE", command=self.read_gcode)
        gcodeButton.grid(row=3, columnspan=2, ipady=10)

        #Entry fields for density and price per gram
        e = Entry(self, justify=CENTER, width=5)
#        e.delete(0, END)
        e.insert(0, "1.13")
        e.grid(row=1, column=0)
        e.bind("<Button-1>", self.delete_entry)
        self.density = float(e.get())


        e_label = Label(self, text="D")
        e_label.grid(row=2, column=0)

        e1 = Entry(self, justify=CENTER, width=5)
#        e1.delete(0, END)
        e1.insert(0, "0.175")
        e1.grid(row=1, column=1)
        self.price = float(e1.get())

        e1_label = Label(self, text="$")
        e1_label.grid(row=2, column=1)

调用delete方法时,它将立即删除小部件中的所有内容。 您的情况是,您在创建小部件后立即将其调用,而无需删除任何内容。

如果要在用户单击条目小部件中的文本时将其删除,则需要定义一个绑定以删除内容。

要创建绑定,请调用小部件的bind方法,并告诉它要绑定的事件和要调用的函数。 例如,如果要调用函数delete_entry ,可以这样进行:

def delete_entry(event):
    event.widget.delete(0, "end")

e = Entry(...)
e.bind("<1>", delete_entry)

当您将函数绑定到事件时,该函数将使用一个参数(代表事件的对象)进行调用。 对象的属性之一是widget ,它是对拥有事件的窗口小部件的引用。 您可以使用此引用与小部件进行交互。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM