As the title implies I'm trying to select items from one list box, press a button, and add it to a second list box.
When I click the button to move, the value is printed in command prompt, but the listbox itself isn't updating.
I copied and pasted so I realize that everything should be tabbed over one spot.
class Actions:
def openfile(self): #select a directory to view files
directory = tkFileDialog.askdirectory(initialdir='.')
self.directoryContents(directory)
def filename(self):
Label (text='Please select a directory').pack(side=TOP,padx=10,pady=10)
files = []
fileListSorted = []
#display the contents of the directory
def directoryContents(self, directory): #displays two listBoxes containing items
scrollbar = Scrollbar() #left scrollbar - display contents in directory
scrollbar.pack(side = LEFT, fill = Y)
scrollbarSorted = Scrollbar() #right scrollbar - display sorted files
scrollbarSorted.pack(side = RIGHT, fill = Y, padx = 2, pady=100)
fileList = Listbox(yscrollcommand = scrollbar.set) #files displayed in the left listBox
for filename in os.listdir(directory):
fileList.insert(END, filename)
global files
self.files.append(filename) #insert the values into the files array so we know which element we want to enter in moveFile
fileList.pack(side =LEFT, fill = BOTH)
scrollbar.config(command = fileList.yview)
global fileListSorted #this is for the filelist in the right window. contains the values the user has selected
fileListSorted = Listbox(yscrollcommand = scrollbarSorted.set) #second listbox (button will send selected files to this window)
fileListSorted.pack(side=RIGHT, fill = BOTH)
scrollbarSorted.config(command = fileListSorted.yview)
selection = fileList.curselection() #select the file
b = Button(text="->", command=lambda:self.moveFile(fileList.curselection()))#send the file to moveFile to be added to fileListSorted
b.pack(pady=5, padx =20)
##moveFile addes files to the array fileLIst2, which is the fileList on the right
def moveFile(self,File):
insertValue = int(File[0]) #convert the item to integer
global files
insertName = self.files[insertValue] #get the name of the file to be inserted
global fileListSorted
self.fileListSorted.append(str(insertName)) #append the value to the fileList array
print self.fileListSorted #second listbox list
It's quite difficult to follow that code -- For example, where is self.fileListSorted defined? -- You have a global fileListSorted and an instance variable self.fileListSorted and they're different things. However, you seem to be getting them confused (for example, why is there a line
global fileListSorted
in moveFile when you never use fileListSorted in there?) Also note that to add items into a ListBox, you typically use the insert method, which you haven't used in moveFiles as far as you've shown anyway...
Related
I am trying to create a framework to create a label, text box and button as an object, I can extend it easily.
Idea:
Original
after extend
and it could extend to have 3, 4, 5 or more if it have more file, just declare in dictionary list, it will extend automatically.
The code I have:
def getpath(entry_box, type):
# path or file
if type == 'path':
entry_box.set(filedial.askdirectory())
elif type == 'file':
entry_box.set(filedial.askopenfilename())
MainWin = tk.Tk() # Create main windows
MainWin.title('Get file and path') # App name
# Create root container to hold Frames
mainFrame = ttk.Frame(MainWin)
mainFrame.grid(column=1, row=1)
# define the object to create in dictionary, format:
# {object name:[title name, default path, button name, file or folder path]}
obj2create ={'file1':['ABC Location: ', r'C:/', 'Get ABC','file'],
'file2': ['DEF Location:', r'C:/1/', 'Get DEF', 'file']}
ttl_obj = 0
for key in obj2create:
ttl_obj +=1
vir = obj2create[key]
# Module for get file:
obj_name = key
title = vir[0]
default_path = vir[1]
btn_name = vir[2]
get_type = vir[3]
# Create main container
pa_frame = ttk.Frame(mainFrame)
pa_frame.grid(column=1, row=10*ttl_obj, sticky=tk.W)
pa_frame.config(width = 100)
# Row 1: Label
frame_name = obj_name + '_name'
print(frame_name)
frame_name = ttk.Label(pa_frame, text= title).grid(column=1, row=10*ttl_obj,sticky=tk.W)
# Row 2: path and button
# assign type
path_loc = obj_name + '_path'
path_loc = tk.StringVar()
path_loc.set(default_path)
# put in frame
fileLocPath = obj_name + '_loc_path'
fileLocPath = ttk.Entry(pa_frame, width=70, textvariable=path_loc)
fileLocPath.grid(column=1, row=30*ttl_obj) # Assign position
# Get file button
# define button display text and assign the command / function
bt_get_file_path = obj_name + '_btn'
bt_get_file_path = ttk.Button(pa_frame, text= btn_name,
command=lambda: getpath(path_loc, get_type))
# Position Button in second row, second column (zero-based)
bt_get_file_path.grid(column=2, row=30*ttl_obj)
# Auto popup when open
MainWin.mainloop() # Let the window keep running until close
Issue:
Default file path appear in second text box only.
All the button location point to second box.
I also not sure how could I get the value in different box, the "path_loc = obj_name + '_path'" not able to get the correct object.
How should I make it work? Or the way I use is wrong?
Tkinter widgets are no different than any other python objects with respect to creating them in a loop. Your problem seems to be that you don't know how to create a unique variable for an unknown number of widgets.
The important thing to know is that you don't need a unique variable for each widget. Instead, you can use a list or dictionary, either of which can be easily extended at runtime.
Saving widget references in a dictionary
Here is a solution using a dictionary:
entries = {}
for key in obj2create:
...
entries[key] = ttk.Entry(...)
...
...
print("the value for file1 is", entries["file1"].get()
Creating a custom compound widget
If you're wanting to create sets of widgets that are to be treated as a group, it may be better to create a class. In effect, you're creating your own custom widget. This type of class is often called a "compound widget" or "megawidget".
For example:
class FileWidget(ttk.Frame):
def __init__(self, parent, name):
ttk.Frame.__init__(self, parent)
self.label = ttk.Label(self, text="%s Location" % name)
self.fileLocPath = ttk.Entry(self)
self.bt_get_file_path = ttk.Button(self, text=name, command=self.get_path)
...
def get_path(self):
return self.fileLocPath.get()
You can then create each row in your GUI like this:
widgets = {}
for key in obj2create:
widgets[key] = FileWidget(mainFrame, key)
widgets[key].pack(side="top", fill="x")
Later, you can get back the values like this:
for key in obj2create:
widget = widgets[key]
print("%s: %s" % (key, widget.get_path())
I am making a simple shopping list application.
Whenever I try to save the content from the listbox shopping_list it saves as a tuple ('.., .., ...,') in the .txt file the function creates.
When I use the open button, the listbox displays the text as a tuple.
Example:
If in the entry field I write something like pizza and add it to the listbox and save it. When I try to open the same file into the listbox, the listbox now displays: ('pizza'). How can I get the listbox to display only pizza?
The code:
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from PIL import ImageTk, Image
def add_item():
'''
This function takes the input from the entry field
and places the item in a shopping list.
:return: Adds to list
'''
if len(user_input.get()) == 0: # Checks if user typed into the entry field.
messagebox.showwarning("Warning", "No keys were detected from the Entry field") # Send warning for no input.
# Else statement for adding the user input to shopping list.
else:
shopping_list.insert(END, user_input.get())
user_input.delete(0, END) # Deletes whatever the user wrote in the entry box after button is clicked.
def remove_item():
'''
This function deletes the selected items when REMOVE button is clicked
'''
try:
selected = shopping_list.curselection()
for item in selected[::-1]: # Fetches all selected items from shopping list
shopping_list.delete(item)
# Make a warning if the shopping list is empty or items are not selected when REMOVE button is clicked.
except TclError:
messagebox.showwarning("Warning", "The shopping list is either empty \nor you didn't select an item")
def open_list():
'''
This function opens an existing shopping list that was previously saved.
:return: Opens older saved files.
'''
root.file = filedialog.askopenfilename(initialdir="./", title="Select a file",
filetypes=(("Txt files", "*.txt"), ("All files", "*,*")))
with open(root.file, "r") as file:
data_content = file.read()
shopping_list.delete(0, END)
shopping_list.insert(END, data_content)
def save_file():
'''
This function saves the whole shopping list and writes the content to the "shopping_list.txt" file.
:return: None
'''
text_file = filedialog.asksaveasfilename(defaultextension=".txt")
try:
with open(text_file, "w") as file:
file.write(str(shopping_list.get(0, END)))
except FileNotFoundError:
messagebox.showinfo("Alert", "No file was saved")
# backup eldre løsning:
"""
def save_file():
'''
This function saves the whole shopping list and writes the content to the "shopping_list.txt" file.
:return: None
'''
root.filename = filedialog.asksaveasfilename()
with open("shopping_list.txt", "w") as f:
for i in shopping_list.get(0, END):
f.write(i+"\n")
"""
# Root files
root = Tk()
root.title("Skoleoppgave OBLIG 5")
root.iconbitmap('favicon.icns')
root.geometry("500x500")
root.resizable(0, 0)
# TODO: Entry widget ENTRY 1
# Creating an Entry widget so user can add items to shopping-list.
user_input = Entry(root, relief=SUNKEN)
user_input.pack()
user_input.place(x=250, y=20, anchor=N)
# Todo: Buttons widget ADD ITEM 2
# Make a Button-widget that adds the user input from entry field to the shopping list.
add_entry = Button(root, text="ADD ITEM", command=add_item, padx=10, pady=5)
add_entry.pack()
add_entry.place(x=158, y=57)
# Todo: Listbox widget 4
# Creating Listbox for shopping list items
shopping_list = Listbox(root, selectmode=EXTENDED)
shopping_list.pack()
shopping_list.place(x=160, y=100)
"""
# Opens last saved shopping list when program starts.
try:
read = open("shopping_list.txt","r")
list_file = read.readlines()
read.close()
for data in list_file:
shopping_list.insert(0, END, data)
except FileNotFoundError:
read = open("shopping_list.txt","w")
"""
# Todo: Buttons widget REMOVE 3
# Make a Button-widget that deletes the selected item from shopping list
remove_entry = Button(root, text="REMOVE", command=remove_item, padx=10, pady=5)
remove_entry.pack()
remove_entry.place(x=160, y=285)
# Todo: Buttons widget OPEN 5
# Make a Button-widget that deletes the selected item from shopping list
open_file = Button(root, text="OPEN", command=open_list, padx=10, pady=5)
open_file.pack()
open_file.place(x=160, y=285)
# Todo: Buttons widget SAVE 6
# Make a Button-widget that deletes the selected item from shopping list
save_file = Button(root, text="SAVE", command=save_file, padx=10, pady=5)
save_file.pack()
save_file.place(x=282, y=57)
root.mainloop()
As mentioned in the comments, you are trying to put a string into a method that is asking for a tuple, that will give an error.
so one way is that you add functionality both in reading and writing to write a data format to the file that you control yourself. tip is trying to replicate one to one what you see in the listbox to the textfile. It looks like your "eldre losning" will put one item per line. So the thing is to read the file so that you get a list of strings where every row becomes an item in the list. Hint... .split on strings works good for that.
Or, just deal with the string you already have in the file.
One way to do that is to add/modify the following.
from ast import literal_eval # added import for safe evals
# modified open
with open(root.file, "rt") as file:
data_content = literal_eval(file.read())
shopping_list.delete(0, END)
shopping_list.insert(END, *data_content)
Side note #1, you don't need .pack() if you use .place.
Side note #2, next time around, mention that you are doing homework upfront.
Side note #3, if you use what's in this answer - you might have to do some reading before you can explain why it could be done like this to be believed that you came up with it yourself :-)
Good luck with your studies and learning.
I have been trying to use tkinter CheckButton widget to edit items in a list - each item of the list is a new checkbutton. I want a save method to save the data to a text file, and a load method to load the info from the text file and mark the checkbutton boxes depending on the items in the list.
Here is my code so far, but the list doesn't seem to change when I check the buttons and update the list/file
Here is my code, I need to know why the list isn't updating when I check the boxes:
import tkinter.messagebox as box
modulesMain = Tk()
moduleChecks = []
def SaveChanges():
# Clear the text file
modules = open("modules.txt", "w") #Write mode to overwrite the whole file
modules.write("") # Put in blank text
modules.close()
modules = open("modules.txt", "a") # Append mode to append the file
for item in moduleChecks:
modules.write(item + "\n")
print(moduleChecks)
appButton = Checkbutton(modulesMain, text = "Test", variable = moduleChecks[0]).grid()
searchButton = Checkbutton(modulesMain, text = "Test", variable = moduleChecks[1]).grid()
Save = Button(modulesMain, text = "Save Changes", command = SaveChanges).grid()
The variable for each of your checkboxes needs to be an IntVar. Your moduleCheck list is currently un-initialised so when you try to access the elements inside it, you'll get an error.
In the below code (modified from yours), I've initialised the moduleCheck to contain two IntVars.
When you press the save button, it will print to the console the current state of the check boxes.
import tkinter as tk
modulesMain = tk.Tk()
moduleChecks = [tk.IntVar() for i in range(2)]
def SaveChanges():
for idx,item in enumerate(moduleChecks):
print(f"{idx} = {item.get()}")
appCheck = tk.Checkbutton(modulesMain, text = "App", variable = moduleChecks[0])
appCheck.grid()
searchCheck = tk.Checkbutton(modulesMain, text = "Check", variable = moduleChecks[1])
searchCheck.grid()
saveButton = tk.Button(modulesMain, text = "Save Changes", command = SaveChanges)
saveButton.grid()
modulesMain.mainloop()
Here is a basic solution.
import tkinter as tk
root = tk.Tk()
#list of options. doubles as checkbox label text
#appending to this list is all that is necessary to create more options
opts = ['App', 'Search']
#create an StringVar for every option
#this way there doesn't have to be type conversions for reading/writing files
vars = [tk.StringVar(value='0') for _ in opts]
#create checkboxes for every option
for opt, var in zip(opts, vars):
tk.Checkbutton(root, text=opt, variable=var, onvalue='1', offvalue='0').grid(sticky='w')
#write the checkbox values to file
def save_options_state():
with open("modules.txt", "w") as file:
file.write(''.join([v.get() for v in vars]))
#set checkbox values from file
def load_options_state():
with open("modules.txt", "r") as file:
for n, var in zip(list(file.read().strip()), vars):
var.set(n)
#save/load buttons
tk.Button(root, text="save", command=save_options_state).grid()
tk.Button(root, text="load", command=load_options_state).grid(row=2, column=1)
root.mainloop()
Here's a runnable example which I think shows how to do everything you've asked about. It assumes the modules.txt file contains data in the following format, where each line consists of a module's name and whether it's checked or not.
Mod1,0
Mod2,0
Mod3,0
The data in the file is used to create a list of Checkbuttons — one for each module — and initially sets to its current status as indicated in the file.
Here's the sample code:
from tkinter import *
import tkinter.messagebox as box
dataFilename = "modules.txt"
moduleChecks = []
def ReadModules():
with open(dataFilename, "r") as file:
for line in (line.strip() for line in file):
moduleName, moduleState = line.split(',')
moduleChecks.append((moduleName, int(moduleState)))
print('read:', moduleChecks)
def SaveChanges():
print('writing:', moduleChecks)
with open(dataFilename, "w") as file:
for checkButton in moduleCheckbuttons:
moduleName = checkButton.cget('text')
moduleState = checkButton.var.get()
file.write(','.join((moduleName, str(moduleState))) + '\n')
ReadModules()
modulesMain = Tk()
moduleCheckbuttons = []
# Create a Checkbutton for each module based on moduleChecks values.
for moduleName, moduleState in moduleChecks:
intVar = IntVar(value=moduleState)
checkButton = Checkbutton(modulesMain, text=moduleName, variable=intVar)
checkButton.var = intVar # Attach reference to variable.
checkButton.grid()
moduleCheckbuttons.append(checkButton)
saveButton = Button(modulesMain, text="Save Changes", command=SaveChanges)
saveButton.grid()
modulesMain.mainloop()
I have a bunch of images which I want the user to select using a scale. As the user updates the scale value, I want the GUI to update which image is shown.
I have just started dealing with GUIs and I'm stuck. I've managed to print the new values from the scale using the command keyword argument of the Scale widget. However, it is not clear how I can get this value to update the image on the interface.
class MainProgram():
def AcquireDicomFiles(self):
# HERE I GET THE IMAGES PATHS. IT WORKS FINE.
def GUI(self):
root_window = Tk()
slice_number = DoubleVar()
bar_length = 200
main_title = Label(root_window, text="Seleção de corte e echo").pack()
scale_slice = Scale(root_window, variable=slice_number, orient=HORIZONTAL, from_=1, to=24, length=bar_length,
cursor="hand", label="Slice Number", command=MainProgram().get_slice_value)
scale_slice.pack(anchor=CENTER)
echo_time = DoubleVar()
scale_echo = Scale(root_window, variable=echo_time, orient=HORIZONTAL, from_=1, to=6, length=bar_length,
cursor="hand", label="Echo Time")
scale_echo.pack(anchor=CENTER)
imagefile = Image.open("image.png")
tk_image = ImageTk.PhotoImage(imagefile)
panel = Label(root_window, image=tk_image).pack(fill="both", expand="yes")
root_window.mainloop()
def get_slice_number(self,user_slice_number):
user_slice_number = np.int(user_slice_number)
def showImage(self):
# Code below works fine for user input in terminal. It uses user_slice_number and user_echo_time to find image. I want these two values to come from the pair of scales
# indexes = np.where(slice_and_echo[:][0] == user_slice_number)[0] #indexes of the elements where the user input match the element
# for i in indexes: #Go through index values. Check and record those in which echo time (element) matches user input
# if slice_and_echo[i][1] == user_echo_time:
# selected_echo_time = user_echo_time
# selected_slice_number = slice_and_echo[i][0]
# index = i
# file_path = os.path.join(dcm_folder, dcm_files[index]) #path of the file whose index match user input
# dcm_read = dicom.read_file(file_path) #read file user wants
# dcm_pixel_values = dcm_read.pixel_array #extract pixel values
slice_and_echo = MainProgram().AcquireDicomFiles()
MainProgram().GUI()
Set echo_time.trace(slider_callback), (which calls the method given to it, whenever the echo_time changes value) and then within slider_callback, you set root_window.iconify(file_name).
I created 12 Entry boxes using a for loop with a default value of N/A. Any change in the text of entry is detected through .trace method.
I want to use reset button to make all the text on the Entry Boxes back to N/A
from tkinter import *
root = Tk()
t_diesel_price_EnF_variable = ["JanVar", "FebVar", "MarVar", "AprVar","MayVar","JuneVar","JulyVar","AugVar","SeptVar", "OctVar", "NovVar", "DecVar"]
t_diesel_price_EnF_values = ["N/A", "N/A","N/A", "N/A","N/A", "N/A","N/A", "N/A","N/A", "N/A","N/A", "N/A"]
def EnFChanging(*events):
for EnF in range(0,len(t_diesel_price_EnF_variable)):
t_diesel_price_EnF_values[EnF]=t_diesel_price_EnF_variable[EnF].get()
try:
t_diesel_price_EnF_values[EnF] = float(t_diesel_price_EnF_values[EnF])
except ValueError:
pass
print(t_diesel_price_EnF_values)
for EnF in range(0,len(t_diesel_price_EnF_values)):
t_diesel_price_EnF_variable[EnF] = StringVar(root , value = "N/A")
t_diesel_price = Entry(root , textvariable = t_diesel_price_EnF_variable[EnF], width = 10).pack()
t_diesel_price_EnF_variable[EnF].trace("w",EnFChanging)
def ChangeText():
for EnF in range(0, len(t_diesel_price_EnF_values)):
t_diesel_price[EnF].delete(0,END)
t_diesel_price[EnF].insert(0,"N/A")
return
b1 = Button(root, text = "Reset" , command = ChangeText).pack()
root.mainloop()
When I press the button it gives an error t_diesel_price[EnF].delete(0,END)
TypeError: 'NoneType' object is not subscriptable
What should I do now, Please ignore the basic errors of programming as I am a Mechanical Engineer with not a programming back ground. And I have to make a lot of other boxes too for my energy calculator.
You trying treat t_diesel_price as an Entry (and as a list) when your variable is None.
First of all I suggest you to install some IDE (e.g. PyCharm) and place break points to see whats wrong with variable!
Your problem occures because you create and pack your widget in one line! So t_diesel_price is None because pack() always returns None (link).
Just split your declaration and packing to:
t_diesel_price = Entry(root , textvariable = t_diesel_price_EnF_variable[EnF], width = 10)
t_diesel_price.pack()
After that it's works for me, except this fact that t_diesel_price is last created entry and the value changes only in it. So I assume that you need another list to iterate over entries:
...
# another fresh list
t_diesel_price_EnF_entries = list()
...
# declare entry
t_diesel_price = Entry(root, textvariable=t_diesel_price_EnF_variable[EnF], width=10)
# pack entry
t_diesel_price.pack()
# append entry to list
t_diesel_price_EnF_entries.append(t_diesel_price)
...
def ChangeText():
# iterate over entries
for diesel_price in t_diesel_price_EnF_entries:
diesel_price.delete(0,END)
diesel_price.insert(0,"N/A")
...
Alternatively you can iterate over StringVar's if you don't wanna store your entries at all:
def ChangeText():
# iterate over stringvars
for EnF in range(len(t_diesel_price_EnF_variable)):
t_diesel_price_EnF_variable[EnF].set('N/A')
And you can make it more readable as in example with entry iterating:
def ChangeText():
# iterate over stringvars
for string_var in t_diesel_price_EnF_variable:
string_var.set('N/A')
Cheers!