通过按钮删除ttk.entry中的内容 - Python

时间:2017-03-29 23:00:23

标签: python-3.x tkinter declaration ttk

我正在尝试清除一个输入字段,但由于某种原因,这个简单的练习是提出一个“NameError:名称'PartDesc'未定义”的消息,它完全逃脱了我出错的地方。我试过转移我的_clear_text,但它仍然在我错误的地方找不到我。

import tkinter
import tkinter.font
from tkinter import ttk
import csv
from csv import DictReader
import sys
import os
import subprocess

tree_columns = ("Drawing", "Issue", "Document type")

class TFPP:

def __init__(self):
    self.tree = None
    self._setup_widgets()
    self._build_tree()
    self._clear_text()


def _setup_widgets(self):
    #this is the setup & layout for the top part of the application i.e. the textbox and buttons.
    frame1=ttk.Frame()
    frame1.pack(fill='both', expand=False)
    AmpicsLabel = ttk.Label(frame1,justify="left", anchor="s", text=("Ampics Number :"))
    SOentry=ttk.Entry(frame1,justify="left", width=15)#, sonumber)
    Searchbutton = ttk.Button(frame1,text="Search")#, command='do_a_search')
    AmpicsLabel.pack(side="left", padx=5)
    SOentry.pack(side="left", padx=5)
    Searchbutton.pack(side="right", padx=3, pady=1)

    frame2=ttk.Frame()
    frame2.pack(fill='both', expand=False)
    DescLabel = ttk.Label(frame2,justify="left", anchor="sw", text=("Part Description : "))
    PartDesc = ttk.Entry(frame2,justify="left", width=57)
    Resetbutton = ttk.Button(frame2,text="Reset", command=self._clear_text)

    DescLabel.pack(side="left", padx=5)
    PartDesc.pack(side="left", padx=5,pady=5)
    Resetbutton.pack(side="right", padx=3, pady=1)

    frame3=ttk.Frame()
    frame3.pack(fill='both', expand=False)
    Quitbutton = ttk.Button(frame3,text="Quit", command=app.destroy)
    Quitbutton.pack(side="right", padx=3, pady=1) 

    # this is the setup & layout for the drawing list part.
    container = ttk.Frame()
    container.pack(fill='both', expand=False)
    self.tree = ttk.Treeview(columns=tree_columns, show="headings")
    vsb = ttk.Scrollbar(orient="vertical", command=self.tree.yview)
    self.tree.grid(column=0, row=0, sticky='nsew', in_=container)
    vsb.grid(column=1, row=0, sticky='ns', in_=container)
    container.grid_columnconfigure(0, weight=1)
    container.grid_rowconfigure(0, weight=1)


def _build_tree(self):
    for col in tree_columns:
        self.tree.heading(col, text=col.title(),
            command=lambda c=col: sortby(self.tree, c, 0))

        self.tree.column("Drawing",width=120,anchor="center", stretch="no")
        self.tree.column("Issue",width=75, anchor="center", stretch="no")
        self.tree.column("Document type",anchor="w",width=300)
        self.treeview=self.tree

def _clear_text(self):
    PartDesc.delete(0, end)

if __name__ == "__main__":
    app = tkinter.Tk()
    app.title("Production Drawings")
    app.geometry("550x330")
    tfpp = TFPP()
    app.mainloop()

1 个答案:

答案 0 :(得分:0)

您应该使用tk.StringVar()来存储Entry的内容。您可以使用get()方法获取它并使用set()方法设置它。要清除它,你可以这样设置

self.my_string = tk.StringVar()
PartDesc = ttk.Entry(frame2, textvariable=self.my_string, justify="left", width=57)
self.my_string.set(‘test’)

功能:

def _clear_text(self):
     self.my_string.set(‘’)

你可以这样做或通过函数传递tk.StringVar()。