python how to select printer for print receipt - python-3.x

the following code is working successfully, i need to print receipts. i want to select printer to print receipts. in this code how can i add printer function. I need to printer selecting option separately.
i don't know which function using in this code.
i hope you will help for this,
Thanks im advance.
from tkinter import ttk
import tkinter as tk
from tkinter import*
def update():
listBox.insert('','end',value=('APL', t1.get(),t2.get(),t3.get()))
listBox.insert('','end',value=('cPL', t4.get(),t5.get(),t6.get()))
def update():
if t1.get() == '' or t2.get() == '' or t3.get() == '':
pass
else:
listBox.insert('','end',value=('APL', t1.get(),t2.get(),t3.get()))
listBox.insert('','end',value=('cPL', t4.get(),t5.get(),t6.get()))
total = 0.0
try:
for child in listBox.get_children():
total += float(listBox.item(child, 'values')[3])
totText.set(total)
except:
pass
lbl = Label(root,text=total,font=('helvetica',21))
lbl.grid(row=5)
def print():
tott = float(totText.get())
top = Toplevel()
top.geometry("300x300")
top.config(bg="white")
l = Label(top, text='-------RECEIPT-------')
l.pack()
l.config(bg="white")
heading = Label(top, text='\tItem\tPRICE\tQTY\tTotal')
heading.pack()
heading.config(bg="white")
for child in listBox.get_children():
item = (listBox.item(child, 'values')[0])
price = float(listBox.item(child, 'values')[1])
qty = float(listBox.item(child, 'values')[2])
tot = float(listBox.item(child, 'values')[3])
item1 = Label(top, text=f'{item}\t{price}\t{qty}\t{tot}')
item1.config(bg="white")
item1.pack()
tot = Label(top, text=f'Total\t{tott}')
tot.config(bg="white")
tot.pack()
root = tk.Tk()
root.geometry('1000x600')
e8 = tk.Label(root,text="APL").grid(row=1,column=0)
t1 = tk.Entry(root)
t1.grid(row=1,column=1)
t2 = tk.Entry(root)
t2.grid(row=1,column=2)
t3 = tk.Entry(root)
t3.grid(row=1,column=3)
e9 = tk.Label(root,text="cPL").grid(row=2,column=0)
t4 = tk.Entry(root)
t4.grid(row=2,column=1)
t5 = tk.Entry(root)
t5.grid(row=2,column=2)
t6 = tk.Entry(root)
t6.grid(row=2,column=3)
global totText
totText = StringVar()
cols = ('Item', 'PRICE', 'QTY', 'Total')
listBox = ttk.Treeview(root, columns=cols, show='headings')
for col in cols:
listBox.heading(col, text=col)
listBox.grid(row=1, column=0, columnspan=2)
listBox.place(x=10, y=300)
b = tk.Button(root,text='Update Listbox',command=update)
b.grid(row=3)
Button(root, text="print", command=print, height=3, width=13).place(x=850, y=120)
root.mainloop()

Related

how to delete a specific item in the list after a button is clicked in tkinter python

Below is a small code where if you click add button a pop-up will appear where you write desired number. The number in the bottom represents the sum of all numbers you entered.
What I am trying to achieve is to update the sum_lbl and index_no as I delete any of the labels.
Code:
from tkinter import *
root = Tk()
root.geometry('400x400')
add_room_area_var= StringVar(None)
area_lst = []
index_no = 0
def destroy(widget):
widget.destroy()
def add_():
add_room_area = Toplevel(root)
add_room_area.title('Add Room area')
add_room_area.wm_minsize(200, 50)
add_room_area.resizable(False, False)
add_room_area.transient(root)
add_r_area_frame = LabelFrame(add_room_area, text=' Room area ', labelanchor=N)
add_r_area_frame.config(padx=3, pady=3)
add_r_area_frame.pack(fill=X, padx=10, pady=10)
add_r_area_entry = Entry(add_r_area_frame, textvariable=add_room_area_var)
add_r_area_entry.pack(fill=X)
add_r_area_entry.focus_set()
while True:
def ok_():
global index_no
name = add_room_area_var.get()
index_no += 1
entry_frame = Frame(root)
index_lbl = Label(entry_frame, text=index_no)
add_room_lbl = Label(entry_frame, text=name, width=12, bg='gray30', fg='white', pady=5)
close_button = Button(entry_frame, text='X', command=lambda:destroy(entry_frame))
entry_frame.pack(anchor=N, padx=1)
index_lbl.pack(side=LEFT, padx=3)
add_room_lbl.pack(fill=X, side=LEFT)
close_button.pack(side=RIGHT)
area_lst.append(int(name))
add_room_area.destroy()
area_sum = sum(area_lst)
sum_lbl.config(text=area_sum)
break
ok_button = Button(add_room_area, text='Ok', command=ok_)
ok_button.pack()
btn = Button(root, text='Add', command=add_)
btn.pack()
sum_lbl = Label(root, font=25)
sum_lbl.pack(side=BOTTOM, pady=15)
root.mainloop()
Output:
After deleting the 3rd and 4th label the output is:
I would suggest to change area_lst to dictionary using the frame as the key and the two labels as the value for each row.
Then update destroy() to use area_lst to update the total and indexes:
from tkinter import *
root = Tk()
root.geometry('400x400')
add_room_area_var= StringVar(None)
area_lst = {} # dictionary to hold labels of each row using frame as the key
def destroy(frame):
frame.destroy()
del area_lst[frame]
update_total()
# update index of remaining rows
for idx, (lbl, _) in enumerate(area_lst.values(), 1):
lbl['text'] = idx
# function to update the total label
def update_total():
area_sum = sum(int(room['text']) for _, room in area_lst.values())
sum_lbl.config(text=area_sum)
def add_():
add_room_area = Toplevel(root)
add_room_area.title('Add Room area')
add_room_area.wm_minsize(200, 50)
add_room_area.resizable(False, False)
add_room_area.transient(root)
add_r_area_frame = LabelFrame(add_room_area, text=' Room area ', labelanchor=N)
add_r_area_frame.config(padx=3, pady=3)
add_r_area_frame.pack(fill=X, padx=10, pady=10)
add_r_area_entry = Entry(add_r_area_frame, textvariable=add_room_area_var)
add_r_area_entry.pack(fill=X)
add_r_area_entry.focus_set()
def ok_():
name = add_room_area_var.get()
entry_frame = Frame(root)
index_lbl = Label(entry_frame, text=len(area_lst)+1)
add_room_lbl = Label(entry_frame, text=name, width=12, bg='gray30', fg='white', pady=5)
close_button = Button(entry_frame, text='X', command=lambda:destroy(entry_frame))
entry_frame.pack(anchor=N, padx=1)
index_lbl.pack(side=LEFT, padx=3)
add_room_lbl.pack(fill=X, side=LEFT)
close_button.pack(side=RIGHT)
# store current row to area_lst
area_lst[entry_frame] = (index_lbl, add_room_lbl)
add_room_area.destroy()
update_total()
ok_button = Button(add_room_area, text='Ok', command=ok_)
ok_button.pack()
btn = Button(root, text='Add', command=add_)
btn.pack()
sum_lbl = Label(root, font=25)
sum_lbl.pack(side=BOTTOM, pady=15)
root.mainloop()
You can allow buttons to call multiple commands, so for your 'close_button' button, I added two more commands: remove the name from 'area_lst' and update the 'sum_lbl' text with the new sum for 'area_lst'
Like this:
from tkinter import *
root = Tk()
root.geometry('400x400')
add_room_area_var= StringVar(None)
area_lst = []
index_no = 0
def destroy(widget):
widget.destroy()
def add_():
add_room_area = Toplevel(root)
add_room_area.title('Add Room area')
add_room_area.wm_minsize(200, 50)
add_room_area.resizable(False, False)
add_room_area.transient(root)
add_r_area_frame = LabelFrame(add_room_area, text=' Room area ', labelanchor=N)
add_r_area_frame.config(padx=3, pady=3)
add_r_area_frame.pack(fill=X, padx=10, pady=10)
add_r_area_entry = Entry(add_r_area_frame, textvariable=add_room_area_var)
add_r_area_entry.pack(fill=X)
add_r_area_entry.focus_set()
while True:
def ok_():
global index_no
name = add_room_area_var.get()
index_no += 1
entry_frame = Frame(root)
index_lbl = Label(entry_frame, text=index_no)
add_room_lbl = Label(entry_frame, text=name, width=12, bg='gray30', fg='white', pady=5)
close_button = Button(entry_frame, text='X', command=lambda:[destroy(entry_frame), area_lst.remove(int(name)), sum_lbl.config(text=sum(area_lst))])
entry_frame.pack(anchor=N, padx=1)
index_lbl.pack(side=LEFT, padx=3)
add_room_lbl.pack(fill=X, side=LEFT)
close_button.pack(side=RIGHT)
area_lst.append(int(name))
add_room_area.destroy()
area_sum = sum(area_lst)
sum_lbl.config(text=area_sum)
break
ok_button = Button(add_room_area, text='Ok', command=ok_)
ok_button.pack()
btn = Button(root, text='Add', command=add_)
btn.pack()
sum_lbl = Label(root, font=25)
sum_lbl.pack(side=BOTTOM, pady=15)
root.mainloop()

Get OptionMenu value after selection and store in array

Here is the code:
from tkinter import *
class Window(Canvas):
def __init__(self,master=None,**kwargs):
Canvas.__init__(self,master,**kwargs)
self.frame = Frame(self)
self.create_window(0,0,anchor=N+W,window=self.frame)
self.row = 1
self.input_n_or_s = []
self._init_entries()
def _init_entries(self):
n_or_s = Label(self.frame, text='N or S', font='Helvetica 10 bold').grid(row = self.row, column = 1)
self.row += 1
def add_entry(self):
n_or_s = ['N', 'S']
variable = StringVar(self.frame)
variable.set(n_or_s[0])
option_n_or_s = OptionMenu(self.frame, variable, *n_or_s)
option_n_or_s.grid(row = self.row, column = 1)
self.row += 1
#def save_entry(self):
if __name__ == "__main__":
root = Tk()
root.resizable(0,0)
root.title('Lot')
lot = Window(root)
lot.grid(row=0,column=0)
scroll = Scrollbar(root)
scroll.grid(row=0,column=1,sticky=N+S)
lot.config(yscrollcommand = scroll.set)
scroll.config(command=lot.yview)
lot.configure(scrollregion = lot.bbox("all"), width=1000, height=500)
def add_points():
lot.add_entry()
lot.configure(scrollregion = lot.bbox("all"))
b1 = Button(root, text = "Add points", command = add_points)
b1.grid(row=1,column=0)
def get_value():
b1.destroy()
lot.save_entry()
b2 = Button(root, text = "Get value!", command = get_value)
b2.grid(row=2,column=0)
root.mainloop()
Can someone help me on what to put inside the 'save_entry()' function to get each values of the OptionMenus (assuming that the 'add entry' button has been clicked more than 5 times), and then put each values in the 'input_n_or_s' array for later use?
For example:
Here's the GUI wherein the user clicked the 'Add points' button 10 times, and then changed some default values to 'S':
My expected output should look like this:
['S', 'N', 'N', 'S', 'N', 'N', 'N', 'S', 'N', 'S']
Here you go I think this is what you want.
from tkinter import *
class Window(Canvas):
def __init__(self,master=None,**kwargs):
Canvas.__init__(self,master,**kwargs)
self.frame = Frame(self)
self.create_window(0,0,anchor=N+W,window=self.frame)
self.row = 1
self.input_n_or_s = []
self._init_entries()
def _init_entries(self):
n_or_s = Label(self.frame, text='N or S', font='Helvetica 10 bold').grid(row = self.row, column = 1)
self.row += 1
def add_entry(self):
n_or_s = ['N', 'S']
self.variable = StringVar(self.frame)
self.variable.set(n_or_s[0])
self.menu = option_n_or_s = OptionMenu(self.frame, self.variable, *n_or_s)
option_n_or_s.grid(row = self.row, column=1)
self.row += 1
def save_entry(self):
print(self.variable.get())
if __name__ == "__main__":
root = Tk()
root.resizable(0, 0)
root.title('Lot')
lot = Window(root)
lot.grid(row=0,column=0)
scroll = Scrollbar(root)
scroll.grid(row=0,column=1,sticky=N+S)
lot.config(yscrollcommand = scroll.set)
scroll.config(command=lot.yview)
lot.configure(scrollregion = lot.bbox("all"), width=1000, height=500)
root.cnt = 0
def add_points():
root.cnt += 1
if root.cnt < 5:
return
root.cnt = 0
lot.add_entry()
lot.configure(scrollregion = lot.bbox("all"))
lot.input_n_or_s.append(lot.variable.get())
b1 = Button(root, text = "Add points", command = add_points)
b1.grid(row=1,column=0)
def get_value():
b1.destroy()
lot.save_entry()
b2 = Button(root, text = "Get value!", command = get_value)
b2.grid(row=2, column=0)
root.mainloop()
I finally figured it out. The reason that I can't manage to make it work before is that I'm trying to get the output from the OptionMenu instead of the variable.
def add_entry(self):
....
self.input_n_or_s.append(variable)
def save_entry(self):
for entry in self.input_n_or_s:
x = str(entry.get())
self.north_or_south.append(x)
print(self.north_or_south)
So here's the whole working code:
from tkinter import *
class Window(Canvas):
def __init__(self,master=None,**kwargs):
Canvas.__init__(self,master,**kwargs)
self.frame = Frame(self)
self.create_window(0,0,anchor=N+W,window=self.frame)
self.row = 1
self.input_n_or_s = []
self.north_or_south = []
self._init_entries()
def _init_entries(self):
n_or_s = Label(self.frame, text='N or S', font='Helvetica 10 bold').grid(row = self.row, column = 1)
self.row += 1
def add_entry(self):
n_or_s = ['N', 'S']
variable = StringVar(self.frame)
variable.set(n_or_s[0])
option_n_or_s = OptionMenu(self.frame, variable, *n_or_s)
option_n_or_s.grid(row = self.row, column = 1)
self.row += 1
self.input_n_or_s.append(variable)
def save_entry(self):
for entry in self.input_n_or_s:
x = str(entry.get())
self.north_or_south.append(x)
print(self.north_or_south)
if __name__ == "__main__":
root = Tk()
root.resizable(0,0)
root.title('Lot')
lot = Window(root)
lot.grid(row=0,column=0)
scroll = Scrollbar(root)
scroll.grid(row=0,column=1,sticky=N+S)
lot.config(yscrollcommand = scroll.set)
scroll.config(command=lot.yview)
lot.configure(scrollregion = lot.bbox("all"), width=1000, height=500)
def add_points():
lot.add_entry()
lot.configure(scrollregion = lot.bbox("all"))
b1 = Button(root, text = "Add points", command = add_points)
b1.grid(row=1,column=0)
def get_value():
b1.destroy()
lot.save_entry()
b2 = Button(root, text = "Get value!", command = get_value)
b2.grid(row=2,column=0)
root.mainloop()
Still, thanks for the effort on helping me, Daniel Huckson. And Bryan Oakley, thanks for your concern also.

How to edit information in tkinter

I'm trying to write a code to get the name, salary, and age and show it in a list. I want this code have the feature of deleteing and editing the information.
But in this step I don't know how delete or edit a particular information.
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *
i = 2
table_row = []
def getting():
global i
i = i + 1
data = []
var = IntVar()
c = Checkbutton(window, variable=var)
c.val = var
data.append(c)
c.grid(row=i, column=0)
value1=e11.get()
data.append(value1)
value2 = e22.get()
data.append(value2)
value3 = e33.get()
data.append(value3)
lblq = Label(window, text=data[1:])
lblq.grid(row=i, column=1)
table_row.append(data)
# print(table_row)
def delete_row():
for Noo, row in reversed(list(enumerate(table_row))):
if row[0].val.get() == 1:
for i in row:
i.destroy()
table_row(Noo)
window = Tk()
window.title("Table with add, edit and delete")
window.geometry('600x600')
def Closing():
res = messagebox.askokcancel("warning", "Are You sure!!!!!")
if res == True:
sys.exit()
############################################### #menu inside winodow
menu = Menu(window)
new_item = Menu(menu)
new_item.add_command(label="Exit", command=Closing)
menu.add_cascade(label='File', menu=new_item)
window.config(menu=menu)
###############################################
lblname =Label(window, text="Name")
lblname.place(x = 440,y = 130)
e11 = Entry(window, width=20)
e11.place(x = 440,y = 150)
lblsurname =Label(window, text="Salary")
lblsurname.place(x = 440,y = 180)
e22 = Entry(window, width=20)
e22.place(x = 440,y = 200)
lblage =Label(window, text="Age")
lblage.place(x = 440,y = 240)
e33 = Entry(window, width=20)
e33.place(x = 440,y = 260)
dl = Button(window, text='Delete',command=delete_row)
dl.place(x = 440,y = 10)
dl = Button(window, text='Edit')
dl.place(x = 440,y = 50)
B2 = Button(window, text="+ add emplyoee", command=getting)
B2.place(x = 440,y = 90)
v0 = StringVar()
e0 = Entry(window, textvariable=v0, state='readonly')
v0.set('Select')
e0.grid(row=1, column=0)
v1 = StringVar()
e1 = Entry(window, textvariable=v1, state='readonly')
v1.set('Information')
e1.grid(row=1, column=1)
window.mainloop()
Maybe I should use Class in my code, but I is difficult for me to use Class and tkinter simultaneously
You could use dictionary to keep single row - it would be more readable
When I add row I create dictionary with widgets and text from Entry, I add to list of rows and clear Entry.
If there is selected row in table then I don't create new row but replace text in selected row.
The same way I search selected row to copy text from row to Entry.
The same way I search selected row to remove row - destroy widgets and delete row on the list.
It is more easier to do it because I use dictionary to keep row and it is more readable.
from tkinter import *
from tkinter import messagebox
from tkinter.ttk import *
# --- functions ---
def closing():
res = messagebox.askokcancel("warning", "Are You sure!!!!!")
if res == True:
sys.exit()
def add_row():
global row_number
# check if there is selected row to update
update = None
for row in table_row:
if row['var'].get() == 1:
update = row
break # dont search next selected row
if update:
row['data'] = [e11.get(), e22.get(), e33.get()]
row['label']['text'] = row['data']
else:
row_number += 1
row = dict()
row['row_number'] = row_number
row['var'] = IntVar()
row['checkbutton'] = Checkbutton(window, variable=row['var'])
row['checkbutton'].grid(row=row_number, column=0)
row['data'] = [e11.get(), e22.get(), e33.get()]
row['label'] = Label(window, text=row['data'])
row['label'].grid(row=row_number, column=1)
table_row.append(row)
# clear entry
e11.delete('0', 'end')
e22.delete('0', 'end')
e33.delete('0', 'end')
lblnumber["text"] = "Row number: -"
def delete_row():
for row in reversed(table_row):
if row['var'].get() == 1:
# remove widgets
row['checkbutton'].destroy()
row['label'].destroy()
# remove row from list
del row
def edit_row():
for row in table_row:
if row['var'].get() == 1:
# remove all text
e11.delete('0', 'end')
e22.delete('0', 'end')
e33.delete('0', 'end')
# put text from row
e11.insert('end', row['data'][0])
e22.insert('end', row['data'][1])
e33.insert('end', row['data'][2])
lblnumber["text"] = "Row number: {}".format(row['row_number'])
break # dont search next selected row
# --- main ---
row_number = 2
table_row = []
window = Tk()
window.title("Table with add, edit and delete")
window.geometry('600x600')
menu = Menu(window)
new_item = Menu(menu)
new_item.add_command(label="Exit", command=closing)
menu.add_cascade(label='File', menu=new_item)
window.config(menu=menu)
lblnumber = Label(window, text="Row Number: -")
lblnumber.place(x=440, y=330)
e00 = Label(window, width=20)
e00.place(x=440, y=150)
lblname = Label(window, text="Name")
lblname.place(x=440, y=130)
e11 = Entry(window, width=20)
e11.place(x=440, y=150)
lblsurname = Label(window, text="Salary")
lblsurname.place(x=440, y=180)
e22 = Entry(window, width=20)
e22.place(x=440, y=200)
lblage = Label(window, text="Age")
lblage.place(x=440, y=240)
e33 = Entry(window, width=20)
e33.place(x=440, y=260)
dl = Button(window, text='Delete', command=delete_row)
dl.place(x=440, y=10)
dl = Button(window, text='Edit', command=edit_row)
dl.place(x=440, y=50)
B2 = Button(window, text="+ add emplyoee", command=add_row)
B2.place(x=440, y=90)
v0 = StringVar()
e0 = Entry(window, textvariable=v0, state='readonly')
v0.set('Select')
e0.grid(row=1, column=0)
v1 = StringVar()
e1 = Entry(window, textvariable=v1, state='readonly')
v1.set('Information')
e1.grid(row=1, column=1)
window.mainloop()

updating python3 tkinter label not working

I can manually update label and entry if i clicked the manual button, but if i clicked the auto button... the console show the random number but the widgets are not updating.
from tkinter import *
import random
import time
def manual_settxt():
for t in range(0,3):
rd = random.randrange(1,100)
labelWidgets[t].configure(text=rd)
entryWidgets[t].delete(0,END)
entryWidgets[t].insert(0,rd)
def auto_settxt():
while True:
time.sleep(3)
for t in range(0,3):
rd = random.randrange(1,100)
print(rd)
labelWidgets[t].configure(text=rd)
entryWidgets[t].delete(0,END)
entryWidgets[t].insert(0,rd)
root = Tk()
namesInput = [1,2,3]
entryWidgets = []
labelWidgets = []
for i in range(0, len(namesInput)):
labelWidgets.append(Label(root,text=namesInput[i],justify='center'))
entryWidgets.append(Entry(root,width=5,justify='center'))
labelWidgets[-1].grid(row=i+1,column=0)
entryWidgets[-1].grid(row=i+1,column=1)
b1 = Button(root, text = "Manual", command=manual_settxt)
b1.grid(row=4,column=0)
b2 = Button(root, text = "Auto", command=auto_settxt)
b2.grid(row=4,column=1)
root.mainloop()
You created an infinite loop. Delete while True.
I refactored some code: used Thread for non-blocking functionality.
Try this
from tkinter import *
import random
import time
from threading import Thread
def manual_settxt():
for index in range(3):
rd = random.randrange(1,100)
labelWidgets[index].configure(text=rd)
entryWidgets[index].delete(0,END)
entryWidgets[index].insert(0,rd)
def job():
for index in range(3):
time.sleep(1)
rd = random.randrange(1,100)
print(rd)
labelWidgets[index].configure(text=rd)
entryWidgets[index].delete(0,END)
entryWidgets[index].insert(0,rd)
def auto_settxt():
Thread(target=job).start()
root = Tk()
namesInput = [1,2,3]
entryWidgets = []
labelWidgets = []
for index, name in enumerate(namesInput):
labelWidgets.append(Label(root, text=name, justify='center'))
entryWidgets.append(Entry(root, width=5, justify='center'))
labelWidgets[-1].grid(row=index+1,column=0)
entryWidgets[-1].grid(row=index+1,column=1)
b1 = Button(root, text = "Manual", command=manual_settxt)
b1.grid(row=4,column=0)
b2 = Button(root, text = "Auto", command=auto_settxt)
b2.grid(row=4,column=1)
root.mainloop()
I use "root.after" and the script seems to be working fine.
from tkinter import *
import random
import time
def manual_settxt():
for index in range(3):
rd = random.randrange(1,100)
labelWidgets[index].configure(text=rd)
entryWidgets[index].delete(0,END)
entryWidgets[index].insert(0,rd)
def auto_settxt():
for index in range(3):
rd = random.randrange(1,100)
print(rd)
labelWidgets[index].configure(text=rd)
entryWidgets[index].delete(0,END)
entryWidgets[index].insert(0,rd)
root.after(1000, auto_settxt)
root = Tk()
namesInput = [1,2,3]
entryWidgets = []
labelWidgets = []
for index, name in enumerate(namesInput):
labelWidgets.append(Label(root, text=name, justify='center'))
entryWidgets.append(Entry(root, width=5, justify='center'))
labelWidgets[-1].grid(row=index+1,column=0)
entryWidgets[-1].grid(row=index+1,column=1)
b1 = Button(root, text = "Manual", command=manual_settxt)
b1.grid(row=4,column=0)
b2 = Button(root, text = "Auto", command=auto_settxt)
b2.grid(row=4,column=1)
root.after(1000, auto_settxt)
root.mainloop()

Tkinter entry boxes

I need to count from a starting input value to an ending value using tkinter entry boxes but the 2 output files are blank except for brackets and I know that the csv outputs work it's just getting the user input that is the issue. Thanks for any help.
There is the current code.
import csv
from tkinter import *
# Variables/Arrays
oddArray = []
evenArray = []
def main():
for i in range(oute1, oute2):
x = i
# Odds
if(x % 2 == 1):
oddArray.append(x)
# Evens
elif(x % 2 == 0):
evenArray.append(x)
with open("odd_labels", "w") as outputFile1:
writer1 = csv.writer(outputFile1)
writer1.writerow([oddArray])
with open("even_labels", "w") as outputFile2:
writer2 = csv.writer(outputFile2)
writer2.writerow([evenArray])
# gui framework
myGui = Tk()
# myGui.geometry('225x50')
# myGui.title("Label Creator")
Label(myGui, text="Starting value: ").grid(row=0)
Label(myGui, text="Ending value: ").grid(row=1)
a = IntVar()
b = IntVar()
e1 = Entry(myGui, textvariable=a).grid(row=0, column=1)
e2 = Entry(myGui, textvariable=b).grid(row=1, column=1)
oute1 = a.get()
oute2 = b.get()
Button(myGui, text="Start", command=main).grid(row=3)
myGui.mainloop()
The problem is how you create your entry fields.
When you use the grid manager at the end of a widget creation then you are unable to use .get() on it because the grid manager is returning None. To avoid this you need to make sure you create your widget first then define where it goes on a separate line.
Change:
e1 = Entry(myGui, textvariable=a).grid(row=0, column=1)
e2 = Entry(myGui, textvariable=b).grid(row=1, column=1)
to:
e1 = Entry(myGui, textvariable=a)
e1.grid(row=0, column=1)
e2 = Entry(myGui, textvariable=b)
e2.grid(row=1, column=1)
I notived your oute1 and oute2 are both only created on the start of the program. You need to move them into a function and it should work fine in your main function.
Try this:
import csv
from tkinter import *
myGui = Tk()
oddArray = []
evenArray = []
Label(myGui, text="Starting value: ").grid(row=0)
Label(myGui, text="Ending value: ").grid(row=1)
a = IntVar()
b = IntVar()
e1 = Entry(myGui, textvariable=a)
e1.grid(row=0, column=1)
e2 = Entry(myGui, textvariable=b)
e2.grid(row=1, column=1)
def main():
oute1 = a.get()
oute2 = b.get()
for i in range(oute1, oute2):
x = i
# Odds
if(x % 2 == 1):
oddArray.append(x)
# Evens
elif(x % 2 == 0):
evenArray.append(x)
with open("odd_labels", "w") as outputFile1:
writer1 = csv.writer(outputFile1)
writer1.writerow([oddArray])
with open("even_labels", "w") as outputFile2:
writer2 = csv.writer(outputFile2)
writer2.writerow([evenArray])
Button(myGui, text="Start", command=main).grid(row=3)
myGui.mainloop()

Resources