如何在python / tkinter中创建清晰的功能?

时间:2016-11-27 11:19:34

标签: python-3.x tkinter

按下Hello按钮后,我无法清除显示的文字。按下清除按钮时,应该清除显示的文本。谁能帮我?代码如下所示。您可以复制粘贴代码以运行,只有清除按钮不起作用。

from tkinter import*
from tkinter import ttk



class Test:

    def button_press(self, value):
        entry_val = self.number_entry.get()
        entry_val += value
        self.number_entry.delete(0, 'end')
        self.number_entry.insert(0, entry_val)

#def button_clear(self):
    #?????

    def __init__(self, root):
        self.entry_value = StringVar(root, value="")
        root.title("Test")
        root.geometry('200x200')
        root.resizable(width=False, height=False)
        style = ttk.Style()
        style.configure('TButton', font='arial', padding=5)
        style.configure('TEntry', font='arial', padding=20)

        self.number_entry = ttk.Entry(root, textvariable = self.entry_value,    width=70)
        self.number_entry.grid(row=0, columnspan=4)

        self.button1 = ttk.Button(root, text='Hello', command=lambda:  self.button_press('Hello')).grid(row=3, column=0)

        self.button_clear = ttk.Button(root, text='clear', command=lambda: self.button_clear()).grid(row=4, column=0)



root = Tk()
calc = Test(root)
root.mainloop()

1 个答案:

答案 0 :(得分:1)

您需要更新 public static void dumpMail() throws FileNotFoundException, IOException, MessagingException, FolderClosedException, SocketTimeoutException { Properties props = new Properties(); String username = "ad\\" + "x"; String password = "y"; props.put("mail.imaps.host", "z"); props.put("mail.imaps.port", "993"); props.put("mail.imaps.connectiontimeout", "10000"); props.put("mail.imaps.timeout", "10000"); props.put("host", "z"); props.put("user", username); props.put("pass", password); Session session = Session.getDefaultInstance(props, null); Store store = session.getStore("imaps"); store.connect("z", username, password); Folder inbox = store.getFolder("inbox"); inbox.open(Folder.READ_ONLY); Flags seen = new Flags(Flags.Flag.SEEN); FlagTerm unseenFlagTerm = new FlagTerm(seen, false); Flags recent = new Flags(Flags.Flag.RECENT); FlagTerm recentFlagTerm = new FlagTerm(recent, true); SearchTerm searchTerm = new AndTerm(unseenFlagTerm, recentFlagTerm); Message[] messages = inbox.search(searchTerm); for (int i = 0; i < 30; i++) { if (messages[i].getSubject().contains("Zeiterfassung")) { System.out.println(messages[i].getContent()); } } } var startArr =[1,2,3,4,5,6,7,8,9]; var startIndex=startArr.indexOf(5);//finds 5, if you meant just because it is the 5th. say startArr=5-1 if (startIndex!=-1){ var a1=startArr.slice(startIndex); var a3=a1.concat(startArr.slice(0,startIndex)); //a3 now contains what you wished for, you may console.log(a3) to see } 命令更改为:

self.entry_value

您不需要将self.button_clear方法结果存储在:

command=lambda: self.entry_value.set('')

如果您需要为其创建方法,则应更新grid

self.button1 = ttk.button(...).grid(...)
self.button_clear = ttk.button(...).grid(...)

通过按钮调用它:

self.entry_value

您的代码应为:

def button_clear(self, event=None):
    self.entry_value.set("")