Python Forum
Converting tkinter listbox into list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting tkinter listbox into list
#1
Hi all,

I am using the below code (PART A) to allow a user to enter words.

Then, I want to save what a user entered into a normal list. Indeed, I need this data as a list for the rest of the project (PART B). You can see in red that, for now, I tried to use the listbox directly, but of course it doesn't work.

How can I save what users are entering into list?

Thanks,

------------------------------PART A
root = tk.Tk()
root.title("Ticker List")
root.geometry("400x400")

def clicked():
    listbox.insert(tk.END, content.get())

def delete():
    listbox.delete(0, tk.END)

def delete_selected():
    listbox.delete(tk.ANCHOR)

# LISTBOX
content = tk.StringVar()
entry = tk.Entry(root, textvariable=content)
entry.pack()

button = tk.Button(root, text="Add Ticker", command=clicked)
button.pack()

button_delete = tk.Button(text="Delete All", command=delete)
button_delete.pack()

button_delete_selected = tk.Button(text="Delete Selected Ticker", command=delete_selected)
button_delete_selected.pack()

listbox = tk.Listbox(root)
listbox.pack()
root.mainloop()
------------------------------------PART B
start = datetime(2020,1,1)
end = datetime(2020,7,31)
stock = [b][color=#C0392B]listbox[/color][/b][u][/u]

df = web.DataReader(stock,'yahoo',start,end)
df.to_excel(f'stockdata_{stock}.xlsx')
buran write Feb-05-2021, 02:29 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Since you have the data in a dataset, you can use pandas to save as json.
see: https://pandas.pydata.org/pandas-docs/st..._json.html
Reply
#3
Hi Larz,

Thanks for the reply! However, I believe that the dataset only comes with PART B of the code and this part is supposed to be feed by the outcome of PART A. However, PART A outcome must be a list for it to work, while it is a listbox for now.

I am new to python and it is the first time I use tkinter so apologies if I seem confused :p

Best,
Reply
#4
Is this an assignment?
Reply
#5
(Feb-06-2021, 12:54 PM)Larz60+ Wrote: Is this an assignment?

Not at all, not a student anymore. I am just trying to mix different stuff I learned. For exemple, PART A of the code above is from a previous project allowing me to Merge PDFs (Youtube tutorial). PART B is from a project teaching how to get info from a website (also Youtube tuto).

Thing is, I enjoy the gui framework from the PDF merger and I would like to use it to handpick data from the web :)
Reply
#6
In PartB, I'm not sure what you are trying to do with the statement stock = [b][color=#C0392B]listbox[/color][/b][u][/u]

That said, here's how I would write it.
You'll have to uncomment PartB and repair.
import tkinter as tk
import pandas as pd
from datetime import datetime


class PartA:
    def __init__(self, parent=None):
        needloop = False
        if parent is None:
            self.parent = tk.Tk()
            self.parent.title("Ticker List")
            self.parent.geometry("400x400")
            needloop = True
        else:
            self.parent = parent

        self.content = tk.StringVar()
        self.listbox = None
        self.create_widgets()

        if needloop:
            self.parent.mainloop()

    def create_widgets(self):
        # LISTBOX
        entry = tk.Entry(self.parent, textvariable=self.content)
        entry.pack()
        
        button = tk.Button(self.parent, text="Add Ticker", command=self.clicked)
        button.pack()
        
        button_delete = tk.Button(text="Delete All", command=self.delete)
        button_delete.pack()

        button_delete_selected = tk.Button(text="Delete Selected Ticker", command=self.delete_selected)
        button_delete_selected.pack()

        self.listbox = tk.Listbox(self.parent)

    def clicked(self):
        self.listbox.insert(tk.END, self.content.get())
    
    def delete(self):
        self.listbox.delete(0, tk.END)
    
    def delete_selected(self):
        self.listbox.delete(tk.ANCHOR)

def main():
    parta = PartA()

    start = datetime(2020,1,1)
    end = datetime(2020,7,31)

    # stock = [b][color=#C0392B]parta.listbox[/color][/b][u][/u]
    # df = web.DataReader(stock,'yahoo',start,end)
    # df.to_excel(f'stockdata_{stock}.xlsx')

if __name__ == '__main__':
    main()
Reply
#7
Larz,

Thanks for the code. I tried, but I did not manage to get it right. However, I solved my problem by:
1 - Saving all user inputs in a text file
2 - Reading the text file and transforming its content into a list
3 - Feeding PART B code with this list

It is longer, but easy to code :p

Thanks for your messages,
Best
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting a list to dictinary tester_V 8 2,637 Jul-02-2021, 09:04 PM
Last Post: tester_V
  Converting list to variables Palves 1 1,734 Sep-18-2020, 05:43 PM
Last Post: stullis
  Trouble with converting list , dict to int values! faryad13 7 3,671 Sep-04-2020, 06:25 AM
Last Post: faryad13
  converting list of zero length to a matrix of 3*3 vp1989 2 1,891 May-20-2020, 07:46 PM
Last Post: deanhystad
  converting string object inside a list into an intiger bwdu 4 2,555 Mar-31-2020, 10:36 AM
Last Post: buran
  more list help converting paul41 3 2,396 Nov-25-2019, 07:59 AM
Last Post: perfringo
  Converting parts of a list to int for sorting menator01 2 2,188 Nov-03-2019, 03:00 PM
Last Post: menator01
  Converting to a list and sort tantony 6 3,134 Oct-07-2019, 03:30 PM
Last Post: perfringo
  Converting List into list of tuples ARV 4 4,679 Sep-28-2019, 04:58 AM
Last Post: perfringo
  Converting List to Libray prophet11 6 3,521 Apr-22-2019, 04:49 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020