【问题标题】:How can I make the "add task" button not accepting empty strings?如何使“添加任务”按钮不接受空字符串?
【发布时间】:2016-04-07 17:01:15
【问题描述】:

以下代码有一个错误,其中“添加任务”按钮将通过在 2 个任务之间创建空格来将空输入添加到列表框中。有没有办法在不可能发生的情况下对此进行编码?我希望列表框中的列表是连续的,其中每个任务一个接一个地出现。我不明白为什么会这样。它是python的一部分吗?再次感谢。

import tkinter as tk


def main():

    root = tk.Tk()
    root.geometry("350x450")
    root.title("basic window")
    root.config(background="azure")
    app = Application (root)
    root.mainloop()


class Application(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent, bg="yellow", bd=2, relief=tk.RIDGE)   
        self.parent = parent
        self.initUI()


    def initUI(self):
        self.pack(fill="both", expand=1)

        #widget layout

        self.listbox = tk.Listbox(self, bd=2, relief=tk.SUNKEN, height="15")
        self.listbox.grid(row=0, column=0, sticky="w", padx=5, pady=5)

        self.entry=tk.Entry(self, width=19)
        self.entry.grid(row=1, stick="w", padx=5)

        self.button1=tk.Button(self, text="add task", command=self.update)
        self.button1.grid(row=1, column=1, sticky="w", padx=5)

        self.label=tk.Label(self, text="stats", bg="yellow")
        self.label.grid(row=0, column=1, sticky="n")

        #programing

    def update(self):

        self.listbox.insert("end", self.entry.get())
        self.entry.delete(0, "end")



if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python tkinter listbox


    【解决方案1】:

    update(self)中,需要测试self.entry.get()是否包含空字符串;如果是,请跳过更新。

    import tkinter as tk
    
    class Application(tk.Frame):
    
        def __init__(self, parent):
            tk.Frame.__init__(self, parent, bg="yellow", bd=2, relief=tk.RIDGE)   
            self.parent = parent
            self.initUI()
    
    
        def initUI(self):
            self.pack(fill="both", expand=1)
    
            #widget layout
    
            self.listbox = tk.Listbox(self, bd=2, relief=tk.SUNKEN, height="15")
            self.listbox.grid(row=0, column=0, sticky="w", padx=5, pady=5)
    
            self.entry=tk.Entry(self, width=19)
            self.entry.grid(row=1, stick="w", padx=5)
    
            self.button1=tk.Button(self, text="add task", command=self.update)
            self.button1.grid(row=1, column=1, sticky="w", padx=5)
    
            self.label=tk.Label(self, text="stats", bg="yellow")
            self.label.grid(row=0, column=1, sticky="n")
    
            #programing
    
        def update(self):
            if self.entry.get() != '':
                self.listbox.insert("end", self.entry.get())
                self.entry.delete(0, "end")
    
    
    def main():
        root = tk.Tk()
        root.geometry("350x450")
        root.title("basic window")
        root.config(background="azure")
        app = Application (root)
        root.mainloop()
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 嗨 Reblochon,代码有效。非常感谢。我想每次有用户输入时都需要这样做。
    • 是的;就目前而言,每次按下按钮时都会测试输入字符串。如果您需要更多的输入字段,并且想要防止输入空字符串,您可以在它们各自的回调方法中使用相同的技术。
    【解决方案2】:
    def update(self):
            if not self.entry.get()== "": #.get gets the value thgat the entry contains
                self.listbox.insert("end", self.entry.get())
                self.entry.delete(0, "end")
    

    另外,我建议使用更好的名称而不是 self.entry,使用 self.intput_txt,当我制作对象时,我倾向于使用 _txt_lbl 等,因为这样更容易看到一目了然它是什么类型的对象

    【讨论】:

    • 嗨,我现在尽量保持简单。低分让我想得更多。哈哈哈我是新程序员。
    • np 任何你没有得到的东西不要犹豫发布
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    相关资源
    最近更新 更多