I'm working on a program that will be used for internally ordering parts between stores. Currently I have the GUI working perfectly, and the code has no errors.
I want to have a button at the bottom of the GUI that will say send, which will read the selections made, and then export those into a file that can be read by the logistics department. I have scoured the internet but cant seem to find such thing for python.
I would prefer it to export the selections into a excel spreadsheet, but text file will do too, if it can be coded so that it would be easy to read.
As I received a bad comment on the last post, I'll post code for the two kinds of selection boxes, i have. The code pasted into a .py file will open a similar GUI.
from tkinter import *
from tkinter.ttk import *
master = Tk()
master.geometry("400x400")
def openiPhone5():
iPhone5 = Toplevel(master)
iPhone5.geometry("800x800")
Label(iPhone5,
text="iPhone 5").grid()
#Variabel til iPhone 5 Farver
iPhone5Colors =('Sort', 'Hvid')
#PARTS###
#OrginalSkærm
OGscreen = Combobox(iPhone5)
OGscreenColor = Combobox(iPhone5)
OGscreen['values'] = (1, 2, 3, 4, 5, "Text")
OGscreenColor['values'] = iPhone5Colors
OGscreen.current(0) # set the selected item
OGscreenColor.grid(column=3, row=7)
#CUSTOM
CustomAmount = Combobox(iPhone5)
CustomTEXT = Combobox(iPhone5)
CustomTEXT['text'] = (1, "Text")
CustomAmount['values'] = (1, 2, 3, 4, 5, "Text")
CustomAmount.current(0) # set the selected item
CustomAmount.grid(column=3, row=18)
CustomTEXT.grid(column=3, row=17)
Custom_lbl = Label(iPhone5,
text="Custom")
Custom_lbl.grid(column=1, row=17)
def openNewWindow1():
# Toplevel object which will
# be treated as a new window
newWindow1 = Toplevel(master)
# sets the title of the
# Toplevel widget
newWindow1.title("Apple")
# sets the geometry of toplevel
newWindow1.geometry("800x800")
# A Label widget to show in toplevel
Label(newWindow1,
text="iPhones").grid()
btn = Button(newWindow1,
text="iPhone 5",
command=openiPhone5)
The simplest way is to start by associating a variable with each combobox. You can then store those variables in a list or dictionary. To get the data out it's just a matter of iterating over the list or dictionary and calling get
on each variable.
Here's an example of a very simplistic example that creates 10 comboboxes with associated variables:
combo_vars = []
for i in range(10):
var = tk.StringVar(root, value=0)
combo_vars.append(var)
label = tk.Label(form, text=f"Value {i+1}:")
combo = ttk.Combobox(form, textvariable=var, width=20, values=list(range(5)))
label.grid(row=i, column=0, sticky="e")
combo.grid(row=i, column=1, sticky="w")
For example, assuming you have a variable named combo_vars
that contains instances of StringVar
such as in the previous example, you can get all of the values and print them out in a simple loop. There's a more concise way of doing this using list comprehensions, but I'm using the more verbose method to make it easier to understand.
values = []
for var in combo_vars:
value = var.get()
values.append(value)
With that, values
has a list of the values. You can then do whatever you want with that list: convert them to a comma-separated list, write them to a database, convert them to json, save them to an excel file, or whatever it is you need to do.
For example, to write a comma-separated list to a file you can use the csv module like so:
import csv
...
with open("data.csv", "w") as csvfile:
writer = csv.writer(csvfile, values)
writer.writerow(values)