Tkinter I want to delete a frame and replace it with another - python

I recently started learning python and even more recently the Tkinter module. For practice I decided to create a window environment that works a bit like mySQL workbench. Everything was fine until I found out that submitting a second query doesn't actually remove the previous one.
My code for the button callback is as follows:
def queryBtnCallBack():
global query
global queryEntry
global resList
global resFrame
j=1
#Delete frame before showing next data
for widget in resFrame.winfo_children():
widget.destroy()
#For this one I also tried
#if resFrame != None:
# resFrame.destroy()
cursor = db.cursor()
resFrame = Tkinter.Frame()
#Check if user has given a correct input
try:
query = queryEntry.get()
cursor.execute(query)
data = cursor.fetchall()
#Let user know if something is wrong
except:
errorQLabel = Tkinter.Label(resFrame, text='Error. Please make sure your query is correct', bg='#99b3e6')
errorQLabel.grid(row=j+1, column=2)
resFrame.configure(background='#99b3e6')
resFrame.pack()
return
for i in data:
resList.append(i)
#Query Results window part
for i in range(len(resList)):
j += 1
resultLabel = Tkinter.Label(resFrame, text=resList[i], bg='#99b3e6')
resultLabel.grid(row=j, column=2)
resFrame.configure(background='#99b3e6')
#Pack the results frame
resFrame.pack()
The way I'm doing it now, it deletes the frame, but then puts the next one underneath the now-deleted one.
Any kind of help would be greatly appreciated.
Thank you in advance

Maybe you're trying to make this more complex than it needs to be. I'd suggest just keeping one big label and reconfiguring it with new text to show the new data, maybe with newlines inserted into the data to keep the sections separate. To get rid of the previous query from the entry widget, just do queryEntry.delete('0', 'end').

Related

Python/TKinter - How to generate a list of selected widgets by name

I've re-written the question for clarity. I'm open to anything- complete redesign if needed.
I am having a problem and trying to start from the ground up since I can't find any solutions that work.
I have a file with a 75 columns, and each column is a feature. I would like the user to be able to select which features to include in the analysis.
Question 1. What is the best widget to use for this?
First, I tried using a listbox, but it wouldn't let me select non-consecutive items except on the default state which required selecting each individual item (there are 75 items so this would be a lot of click).
Next, I tried using checkboxes. I think this is the best solution, but I'm having problems implementing. Here's what the UI looks like:
I am trying to associate this with a list of 'clicked' boxes that I can then pass to my back-end to remove unwanted variables since the rest of the application is pretty data intensive.
The select all and deselect all buttons work fine; my problem is with the individual selections.
Is this the right way to accomplish this goal at all? If so, how might this be accomplished? TIA- I started using tkinter yesterday so I know very little.
Here is how I'm generating the below (simplified)
Code that creates the button:
import tkinter as tk
settings.data_included_cols = ['button1'] #This is the list of clicked buttons
checkbox_buttons=dict()
checkbox_variables=dict()
button_names=['button1', 'button2', 'button3']
i=0
for i in range(len(button_names)):
checkbox_variables[i]=1
checkbox_button[i] = tk.Checkbutton(frame, text=button_names[i],
variable=checkbox_variables[i],
command=checkbox_click)
Command Code (checkbox_click)- I don't know what goes here but nothing I've tried so far has worked. Originally I was calling it as: command=lambda: checkbox_click(i), and it tried to work like the below:
def checkbox_click(i):
if i in settings.data_included_cols:
settings.data_included_cols.remove(button_name[i])
This doesn't work because 'i' is not associated with the button, it's associated with the loop so it will always be whatever the final value is (in this case 3 which maps to button3).
Any thoughts?
Checkboxes do not need to have a command specified.
They do however need a StringVar or IntVar associated with them, so you can query the values.
So I would build your code like this:
names = {
"id", "member_id", "loan_amnt"
}
values = {}
boxes = {}
for name in names:
values[name] = tk.Intvar(value=1)
boxes[name] = ttk.Checkbox(root, text=name, variable=values[name])
Once the user has made submitted their choice, you can query the dict of values to see which options were selected.
The problem is that although you assign each check-button different "feature" as you create them, when one of the buttons is clicked, the program will execute the function
lambda: feature_list(feature)
where it will try to get the value of "feature", and will find that it equal to the last one in the list.
The seemingly only solution is to assign each check-button a variable, such as something like this:
import tkinter as tk
from tkinter import ttk
def click():
temp = []
for i in range(3):
if value[i].get() == 1:
temp.append(feature[i])
print(temp)
if __name__ == "__main__":
root = tk.Tk()
feature = ["one", "two", "three"]
check = {}
value = {}
for i in range(3):
value[i] = tk.IntVar(value=0)
check[i] = ttk.Checkbutton(root, text=feature[i], variable=value[i], onvalue=1, offvalue=0, command=click)
check[i].grid(row=0, column=i)
root.mainloop()
then when you select some buttons, for example "one" and "three", then it will print
['one', 'three']
You should not be stingy for creating Tk variables, since the space consumed by a check-button should be much bigger than that consumed by a variable. Also, creating variables to get the state of widgets is the most "standard" and "Pythonic" way which you should always use.

Simplifying this Tkinter For Loop to Update Entry Values between two columns

This script works fine for doing what I want it to, but I think there's likely a far more direct alternative that I'm missing.
All I have is a tkinter checkbox that when pressed, runs this function that maps all the info from column 0's rows of entry boxes into column 1's entry boxes. Currently, this is the script:
def update_company_new():
company = str()
for child in frame.winfo_children():
if child.grid_info()["column"] == 0:
company = child.get()
if child.grid_info()["column"] == 1:
child.insert(0, company)
Is there a more direct way to do this? This seems something that could normally be done with a simple list comprehension but I can't find enough details on additional options for winfo_children() and grid_info() that make it adaptable to tkinter grid references.
It can be done using list comprehension:
[frame.grid_slaves(row=w.grid_info()['row'], column=1)[0].insert(0, w.get()) for w in frame.grid_slaves(column=0)]
But it is hard to understand for beginner and it created an useless list of None.
It is better to use a simple for loop instead:
for w in frame.grid_slaves(column=0):
row = w.grid_info['row']
peer = frame.grid_slaves(row=row, column=1)
# check if there is Entry in column 1
if peer:
peer[0].insert(0, w.get())

Learning python - looking to display results loaded from a DB into a gui

I am learning python and Tkinter for a specific project. I have a little programing background, but its been a long while.
I am able to load my DB into an array without trouble. I am struggling to find code examples to help me with the GUI portion.
What I am trying to do is open a GUI with Tkinter and have an input field to search an SQL database.
--> input [select * from db where name like '%bob%'] [ go ]
on button press [go], load results from the SQL query (which is first red into an array)
I am having trouble figuring out how to approach this.
def main():
frame = Tk()
frame.geometry("480x360")
##////////////////////////////
display = Label(frame, text="")
display.grid(row=5, column=1)
##////////////////////////////
def go():
try:
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=WINx;DATABASE=dbx;Trusted_connection=yes')
cursor = conn.cursor()
cursor.execute("SELECT * FROM SalesLT.Customer")
for row in cursor.fetchall():
display.configure(text=row)
print(row) ## ONLY prints the last row in DB
conn.close()
except:
display.configure(text="FAILURE")
Button(frame, text="10p", command=lambda: go().grid(row=3, column=1)
frame.mainloop()
main()
Any good advice for solving this problem?
My end goal is to have a user "select" a database field and do something with that selection. But I'm not there yet.
Perhaps a biased answer, but try to work through the tutorial at tkdocs.com to get yourself familiar with Tkinter. Even from the initial examples you'll get a feel for how to structure your application.

How to update layers of tkinter widgets dynamically?

Tkinter Requirements
So I am relatively new to using tkinter and I am struggling with a very specific doubt here. I tried finding solutions to this but as much as I find it obvious, the solution to this doesn't seem to be easy to understand. So if you can see the image above, I am trying to create a GUI for a particular project which requires multi-layer (I am calling it 3D array based) widgets.
Let's say the variables used for this pointer system are i, j, and k.
I am creating individual layer widgets using for loop:
for n in range(i):
frame_x[i] = Frame(root).grid(row = 1, column = i)
entry_x[i] = Entry(frame_x[i]).grid(row = 2, column = i)
button_x[i] = Button(frame_x[i]).grid(row=3, column = i)
Please note this is not a functional code, I have tried to keep it to the point just to give an idea of the method I am using. (Let me know if you want a more detailed code block.)
Now coming to the problem. I am able to do the basic part of this. But the problem is that I want it to work dynamically.
Let's say if the user enters j = 4 first. 4 blocks will be created.
Later if he changes the value to j = 2 and the presses the button, ideally it should make the widgets at block j= 3 and 4 disappear. But I guess tkinter works on overlapping basis and doesn't change a grid element until something is specifically overlapped over it. How do I do that. I tried destroying the entire frame just after entering the for loop, but that doesn't work as for the first time no widget is created before destroying and python throws NameError saying I can't use a variable before assignment.
Anyways, please let me know how do I do this efficiently.
And also in general, if there is a better way to go about the whole thing. Please refer the image above and let me know if it doesn't make sense.
I am not very comfortable with classes in general. I prefer the inefficient way by only using functions to do everything I have to. So it would be great if you can share me a framework without using classes. But its okay if you use them. I know I should start working with classes at some point.
First off, I want to address this part of the question:
I guess tkinter works on overlapping basis and doesn't change a grid element until something is specifically overlapped over it.
I'm not entirely sure what you mean by that, but if it means what I think it means, it is a false statement. tkinter doesn't "work on an overlapping basis". If you destroy a widget, it is destroyed. It doesn't matter if it's overlapped or not.
Based on the tiny bit of code you posted, the main problem is that you aren't putting the entry and button in the frame. Because of that, they are not destroyed when you destroy the frame.
The reason you aren't putting the widgets into the frame is because of this line:
frame_x[i] = Frame(root).grid(row = 1, column = i)
In python, when you do x=y().z(), x has the value of z(). Thus, when you do frame_x[i] = Frame(...).grid(...), frame_x[i] has the value of .grid(...), and .grid(...) always returns None. Thus, frame_x[i] will be None.
When you next do entry_x[i] = Entry(frame_x[i]).grid(...), it's the same as doing entry_x[i] = Entry(None).grid(...). Because the master of the Entry is None, it becomes a child of the root window.
So, the first step is to separate the creation of the widget from the layout of the widget.
frame_x[i] = Frame(root)
frame_x[i].grid(row = 1, column = i)
Once you do that, the Entry and Button widgets will become a child of the frame, and you can remove widgets you don't want by destroying the frame (eg: frame_x[i].destroy()), since destroying a widget will also cause all children of the widget to be destroyed.
Once you have that in place, you can destroy unwanted widgets by simply calling .destroy() on the frame. For example, if you have previously created 10 groups and now need only 5, you can destroy the others and then remove them from the list like this:
# assume 'num' contains the number of frames that we want,
# and that it is smaller than the number of items in frames_x
for frame in frames_x[num:]:
frame.destroy()
frames_x = frames_x[:num]
Here is a complete working program to illustrate. Enter a number and click the button. It will create that many frame+entry+button combinations. Enter a new number that is larger or smaller and it will either add or remove widgets.
This would be easier if you used classes, but you specifically asked for a solution that doesn't use classes. In your real code you probably need to also save the entry widgets in an array so that you can reference them later, but this example is focuses on the creation of the widgets rather than writing your whole program for you.
import tkinter as tk
frames_x = [] def create_widgets():
global frames_x
num = int(num_widgets.get())
# if the number is less than the previous number of
# widgets, delete the widgets we no longer want
for frame in frames_x[num:]:
frame.destroy()
frames_x = frames_x[:num]
# if the number is greater than the previous number of
# widgets, create additional widgets
for i in range(len(frames_x), num):
# create new widget
frame = tk.Frame(root, bd=1, relief="raised")
entry = tk.Entry(frame)
button = tk.Button(frame, text="click me")
# pack entry and button in frame
button.pack(side="right")
entry.pack(side="left", fill="x", expand=True)
# grid the frame in the parent
frame.grid(row=i+1, column=0, columnspan=2)
# save the new frame in the array
frames_x.append(frame)
root = tk.Tk() num_widgets = tk.Entry(root) button = tk.Button(root, text="Create widgets", command=create_widgets)
button.grid(row=0, column=1) num_widgets.grid(row=0, column=0, sticky="ew")
root.mainloop()

Tkinter: Update frequency of Listbox

Is it possible to control the update frequency of the Listbox widget? Right now I do a lot of insert and delete operations at a high frequency and the Listbox doesn’t refresh very well. Maybe there is a way to override some draw function of the Listbox to fix this issue?
I am not able to find a way to disable visual updates of your listbox so I had to build a work around. If someone knows if you can disable the visual update of listbox please let me know.
My workaround will involve a list and 2 functions.
My first function will take the data that is going to be added to the listbox and instead add it to a list. This function simply simulates new values being added faster than what we want to update for a good visual on the method. You can adapt this code to yours to see how it will work with your inserts.
My second function will run once a second and take all the new values of this list and add them to the listbox by index.
This is a simple example but it should be a good starting point for you.
import tkinter as tk
root = tk.Tk()
add_tracker = 1
new_lb_items = []
lb = tk.Listbox(root)
lb.pack()
def add_to_listbox():
global add_tracker, new_lb_items, root
new_lb_items.append([add_tracker, "Number {}".format(add_tracker)])
add_tracker += 1
root.after(250, add_to_listbox)
def update_listbox_display():
global lb, new_lb_items, root
for item in new_lb_items:
lb.insert(item[0], item[1])
new_lb_items = [] # resets the list so only new values are added next time.
root.after(1000, update_listbox_display)
add_to_listbox()
update_listbox_display()
root.mainloop()

Categories