Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 条目小部件:避免使用多个搜索栏_Python_Tkinter_Tkinter Entry - Fatal编程技术网

Python 条目小部件:避免使用多个搜索栏

Python 条目小部件:避免使用多个搜索栏,python,tkinter,tkinter-entry,Python,Tkinter,Tkinter Entry,在我的代码中有两个按钮——当我点击第一个按钮时,程序写入窗口“主页”,第二个按钮写入窗口“搜索”,并在“搜索”下创建搜索栏。我的问题是,当我点击搜索按钮两次(或更多次)时,搜索栏也会创建更多次。我怎样才能修好它?(我总是希望只有一个搜索栏) 在这里,您只需添加一个变量,该变量表示应用程序的条目是否已创建: class App(): def __init__(self): self.window = Tk() self.text=Label(se

在我的代码中有两个按钮——当我点击第一个按钮时,程序写入窗口“主页”,第二个按钮写入窗口“搜索”,并在“搜索”下创建搜索栏。我的问题是,当我点击搜索按钮两次(或更多次)时,搜索栏也会创建更多次。我怎样才能修好它?(我总是希望只有一个搜索栏)


在这里,您只需添加一个变量,该变量表示应用程序的条目是否已创建:

class App():
    def __init__(self):     
        self.window = Tk()

        self.text=Label(self.window, text="Some text")
        self.text.pack()
        button_home = Button(self.window, text='Home',command= self.home)
        button_home.pack()
        button_search = Button(self.window, text='Search', command=self.search)
        button_search.pack()

        self.has_entry = False

    def home(self):
        self.text['text'] = 'home'

    def search(self):
        self.text["text"] = 'search'
        if not self.has_entry:
            self.meno = StringVar() # NOTE - change meno to self.meno so you can  
                                    # access it later as an attribute
            m = Entry(self.window, textvariable=self.meno).pack()
            self.has_entry = True
更进一步,您可以让home和search按钮控制是否实际显示条目小部件。您可以使用条目的
.pack
.pack\u-forget
方法执行此操作:

class App():
    def __init__(self):     
        self.window = Tk()

        self.text=Label(self.window, text="Some text")
        self.text.pack()
        button_home = Button(self.window, text='Home',command= self.home)
        button_home.pack()
        button_search = Button(self.window, text='Search', command=self.search)
        button_search.pack()

        self.meno = StringVar()
        self.entry = Entry(self.window, textvariable=self.meno)

    def home(self):
        self.text['text'] = 'home'
        self.entry.pack_forget()

    def search(self):
        self.text["text"] = 'search'
        self.entry.pack()

希望这有帮助

谢谢,这很有帮助。
class App():
    def __init__(self):     
        self.window = Tk()

        self.text=Label(self.window, text="Some text")
        self.text.pack()
        button_home = Button(self.window, text='Home',command= self.home)
        button_home.pack()
        button_search = Button(self.window, text='Search', command=self.search)
        button_search.pack()

        self.meno = StringVar()
        self.entry = Entry(self.window, textvariable=self.meno)

    def home(self):
        self.text['text'] = 'home'
        self.entry.pack_forget()

    def search(self):
        self.text["text"] = 'search'
        self.entry.pack()