Reputation: 9
How can I make it so that whenever the button is pressed it runs again. i.e. I enter "BIG" and click button -> returns answer, I then change "BIG" to "SMALL" and click again but nothing happens but I want it to search again and return a result again.
from tkinter import *
def part_search():
Finder = ()
scrollbar = Scrollbar(mGui)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(mGui)
listbox.pack(fill=BOTH, expand=1)
listbox.configure(justify=CENTER, bg="orange", font="Times 16")
mtext = ment.get()
mlabel2 = Label(mGui, text=mtext)
mlabel2.pack()
msearch = open(r"D:\Users\gblmac\Desktop\Python programs\Parts List\Parts.txt")
for line in msearch:
if mtext in line:
listbox.insert(END, str(line))
listbox.config(yscrollcommand=scrollbar.set, height=30, width=0, justify=LEFT)
scrollbar.config(command=listbox.yview)
msearch.close()
return
mGui = Tk()
mGui.configure(bg="orange")
ment = StringVar()
mGui.geometry ("1000x500+200+300")
mGui.title ("Parts Finder")
mlabel1 = Label(mGui, text="Type in search term", font="Times 20", bg="orange")
mlabel1.pack()
mentry = Entry(textvariable=ment, font="Times 20", width = 50, bg="orange")
mentry.pack()
mentry.focus()
mlabel3 = Label(mGui, text="Now click GO", font="Times 20", bg="orange")
mlabel3.pack()
mbutton = Button(mGui, text ="Go", font="Times 20", command = part_search, bg="grey")
mbutton.pack()
Upvotes: 0
Views: 894
Reputation: 1528
Well, you have some problems in creating elements.
I will first provide the fixed code and then explain:
from tkinter import *
def part_search():
# The sole purpose of this function is to update elements, not creating them.
mtext = ment.get()
print(mtext)
mlabel2.config(text=mtext)
msearch = open(r"Parts.txt")
print('again')
for line in msearch:
if mtext in line:
listbox.insert(END, str(line))
listbox.config(yscrollcommand=scrollbar.set,width=0, justify=LEFT)
scrollbar.config(command=listbox.yview)
msearch.close()
return
# This code is your original code for creating the first elements
mGui = Tk()
mGui.configure(bg="orange")
ment = StringVar()
mGui.geometry ("1000x500+200+300")
mGui.title ("Parts Finder")
mlabel1 = Label(mGui, text="Type in search term", font="Times 20", bg="orange")
mlabel1.pack()
mentry = Entry(textvariable=ment, font="Times 20", width = 50, bg="orange")
mentry.pack()
mentry.focus()
mlabel3 = Label(mGui, text="Now click GO", font="Times 20", bg="orange")
mlabel3.pack()
mbutton = Button(mGui, text ="Go", font="Times 20", command = part_search, bg="grey")
mbutton.pack()
# here is the moved code from the function
Finder = ()
scrollbar = Scrollbar(mGui)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(mGui)
listbox.pack(fill=BOTH, expand=1)
listbox.configure(justify=CENTER, bg="orange", font="Times 16")
mlabel2 = Label(mGui)
mlabel2.pack()
while True:
# ball.draw()
mGui.update_idletasks()
mGui.update()
What happened?
In your code, inside the part_search
function, you've created elements and displayed them inside the UI. Then, each call, you create those elements again and stack them in your layout. They have just disappeared and were below you listbox.
Another thing is that you've set a full height listbox which pushed your Label below the bottom of the window.
You should use the Tk
layout schemes (Stacking, griding, etc.). See official documentation here
How can I avoid this type of errors?
You should separate the code that creates the UI and the code that provide it with information and reacts to the user actions. A quick search in YouTube and basic tutorials should get you started. It is a lot easier to solve one problem at a time, and there are many patterns and knowledge about UI design, it is really useful even for backend developers and researchers that only creates UIs for POCs.
Good luck!
Upvotes: 1