在 Tkinter 中为自动完成条目小部件设置默认文本

我正在使用具有一些简单字段的 tkinter 构建一个 GUI。我想出了如何使用 Entry 小部件的 textvariable 参数为它们默认值(我将这些值从 .pickle 文件加载到字典),但是我构建了一个字段以在用户输入值时使用自动完成. 这是一个单独的类,我使用了在这里找到的代码。


我已经尝试了很多方法来在启动应用程序时加载这个小部件中的值,比如返回值和使用 set() 方法,但我没有成功。我要么得到错误,要么什么也没发生。我在小部件中输入的值确实会成功保存到我的泡菜文件中。


我还在用python学习面向对象编程,所以请放轻松。那么如何修改下面的代码以显示默认值?


from Tkinter import *

import re


lista = ['a', 'actions', 'additional', 'also', 'an', 'and', 'angle', 'are', 'as', 'be', 'bind','bracket', 'brackets', 'button', 'can', 'cases', 'configure', 'course', 'detail', 'enter', 'event', 'events', 'example', 'field', 'fields', 'for', 'give', 'important', 'in', 'information', 'is', 'it', 'just', 'key', 'keyboard', 'kind', 'leave', 'left', 'like', 'manager', 'many', 'match', 'modifier', 'most', 'of', 'or', 'others', 'out', 'part', 'simplify', 'space', 'specifier', 'specifies', 'string;', 'that', 'the', 'there', 'to', 'type', 'unless', 'use', 'used', 'user', 'various', 'ways', 'we', 'window', 'wish', 'you']



class AutocompleteEntry(Entry):

    def __init__(self, lista, *args, **kwargs):


        Entry.__init__(self, *args, **kwargs)

        self.lista = lista        

        self.var = self["textvariable"]

        if self.var == '':

            self.var = self["textvariable"] = StringVar()


        self.var.trace('w', self.changed)

        self.bind("<Right>", self.selection)

        self.bind("<Up>", self.up)

        self.bind("<Down>", self.down)


        self.lb_up = False



潇潇雨雨
浏览 104回答 2
2回答

青春有我

有一个现有的 AutocompleteEntry 小部件。例子:from ttkwidgets.autocomplete import AutocompleteEntryimport tkinter as tkwindow = tk.Tk()Label1 = tk.Label(window, text="Entry:")Label1.pack(side='left', padx=10, pady=10)test_values = ['one', 'two', 'three']test_var = tk.StringVar(window)test_var.set('Default Value')Entry1 = AutocompleteEntry(window, width=20, textvariable=test_var, completevalues=test_values)Entry1.pack(side='left', padx=10, pady=10)window.mainloop()

小唯快跑啊

好的,在分析了这段代码几个小时后,我想出了如何使用该insert()方法将值设为默认值。这样做后我的第一个问题是列表框在应用程序启动时默认也是活动的。通过向changed函数添加另一个 if 条件,它不会打开列表框,直到值被清除。当我添加条件语句时,该行在self.lb.destroy()清除字段时会引发错误:AttributeError:“AutocompleteEntry”对象没有属性“lb”我不确定为什么会抛出这个错误,因为它在我添加条件之前就起作用了。但是,lb未在类或函数的其他任何地方定义。删除该行修复了错误,一切似乎都按预期工作。这是我所做的更改。如果其他人有更好的解决方案,请告诉我。class AutocompleteEntry(Entry):&nbsp; &nbsp; # add settings argument which is True or False&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; def __init__(self, lista, settings, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; Entry.__init__(self, *args, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; self.lista = lista&nbsp; &nbsp; &nbsp; &nbsp; # define the self.settings object&nbsp; &nbsp; &nbsp; &nbsp; self.settings = settings&nbsp; &nbsp; &nbsp; &nbsp; self.var = self["textvariable"]&nbsp; &nbsp; &nbsp; &nbsp; if self.var == '':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.var = self["textvariable"] = StringVar()&nbsp; &nbsp; &nbsp; &nbsp; self.var.trace('w', self.changed)&nbsp; &nbsp; &nbsp; &nbsp; self.bind("<Return>", self.selection)&nbsp; &nbsp; &nbsp; &nbsp; self.bind("<Up>", self.up)&nbsp; &nbsp; &nbsp; &nbsp; self.bind("<Down>", self.down)&nbsp; &nbsp; &nbsp; &nbsp; self.lb_up = False&nbsp; &nbsp; def changed(self, name, index, mode):&nbsp; &nbsp; &nbsp; &nbsp; if self.var.get() == '':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # self.lb.destroy() - removed this line&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb_up = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # change this variable to False once the field is cleared&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.settings = False&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # add if condition - field is not empty and settings is True&nbsp; &nbsp; &nbsp; &nbsp; elif self.var.get() != '' and self.settings == True:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb_up = False&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; words = self.comparison()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if words:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not self.lb_up:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb = Listbox()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb.bind("<Double-Button-1>", self.selection)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb.bind("<Return>", self.selection)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb.place(x=self.winfo_x(), y=self.winfo_y()+self.winfo_height())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb_up = True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb.delete(0, END)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for w in words:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb.insert(END,w)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.lb_up:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb.destroy()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.lb_up = False最后一部分:if __name__ == '__main__':&nbsp; &nbsp; root = Tk()&nbsp; &nbsp; # define settings variable and add to AutoCompleteEntry arguments&nbsp; &nbsp; settings = True&nbsp; &nbsp; entry = AutocompleteEntry(lista, settings, root)&nbsp; &nbsp; entry.insert(END, "this is the default value")&nbsp; &nbsp; entry.grid(row=0, column=0)&nbsp; &nbsp; Button(text='nothing').grid(row=1, column=0)&nbsp; &nbsp; Button(text='nothing').grid(row=2, column=0)&nbsp; &nbsp; Button(text='nothing').grid(row=3, column=0)&nbsp; &nbsp; root.mainloop()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python