I am trying a test application with Tkinter. I have created a table using grid layout manager as shown below. There are two buttons - Add row and Delete row. Add row works as expected. How i can go about deleting selected rows. My idea was to give a Checkbutton for each row as shown below. And every selected row can be deleted. But i don't know how to do it exactly and if its possible.
Is there a better way to delete the rows in this case ? please suggest a solution.
CODE:
from Tkinter import *
import ttk
from ttk import *
i =2
def add_row():
global i
var = IntVar()
c = Checkbutton(root, variable = var)
c.grid(row = i, column = 0)
for j in range(1,5): #Columns
b = Entry(root)
b.grid(row=i, column=j)
i =i+1
root = Tk()
bt = ttk.Button(root , text = 'Add Row', command = add_row)
bt.grid(row =0, column=0)
dl = ttk.Button(root , text = 'Delete Row')
dl.grid(row =0, column=1)
v0 = StringVar()
e0 = Entry(root, textvariable = v0, state = 'readonly')
v0.set('Select')
e0.grid(row = 1, column = 0 )
v1 = StringVar()
e1 = Entry(root, textvariable = v1, state = 'readonly')
v1.set('Col1')
e1.grid(row = 1, column = 1 )
v2 = StringVar()
e2 = Entry(root, textvariable = v2, state = 'readonly')
v2.set('Col2')
e2.grid(row = 1, column = 2)
v3 = StringVar()
e3 = Entry(root, textvariable = v3, state = 'readonly')
v3.set('Col3')
e3.grid(row = 1, column = 3 )
v4 = StringVar()
e4 = Entry(root, textvariable = v4, state = 'readonly')
v4.set('Col4')
e4.grid(row = 1, column = 4 )
mainloop()
Note: I do not want to use tktable or treeview to create tables.
In order to delete widgets, you need to keep a reference to them when they are created.
In the code below, I have created a list rows which has a sub-list items for each row. Each row contains a reference to the checkbutton and all the entries.
When Delete Row is pressed, the function loops through the list and destroys all items in the row that has the checkbutton activated, then removes the entry from the list.
Notes:
I have made var an attribute of the checkbutton, so that we can access it to see if it has been checked - see this question.
You were creating your checkbutton five times for each row; I've taken it out of the loop.
I iterate through the list of rows backwards, so that when an item is pop-ed, it only changes the index for the rows already processed. This means that you can delete multiple rows at once.
from Tkinter import *
import ttk
from ttk import *
i=2
rows = []
def add_row():
global i
i=i+1
items = []
var = IntVar()
c = Checkbutton(root, variable = var)
c.val = var
items.append(c)
c.grid(row = i, column = 0)
for j in range(1,5): #Columns
b = Entry(root)
items.append(b)
b.grid(row=i, column=j)
rows.append(items)
def delete_row():
for rowno, row in reversed(list(enumerate(rows))):
if row[0].val.get() == 1:
for i in row:
i.destroy()
rows.pop(rowno)
root = Tk()
bt = ttk.Button(root , text = 'Add Row', command = add_row)
bt.grid(row =0, column=0)
dl = ttk.Button(root , text = 'Delete Row', command = delete_row)
dl.grid(row =0, column=1)
v0 = StringVar()
e0 = Entry(root, textvariable = v0, state = 'readonly')
v0.set('Select')
e0.grid(row = 1, column = 0 )
v1 = StringVar()
e1 = Entry(root, textvariable = v1, state = 'readonly')
v1.set('Col1')
e1.grid(row = 1, column = 1 )
v2 = StringVar()
e2 = Entry(root, textvariable = v2, state = 'readonly')
v2.set('Col2')
e2.grid(row = 1, column = 2)
v3 = StringVar()
e3 = Entry(root, textvariable = v3, state = 'readonly')
v3.set('Col3')
e3.grid(row = 1, column = 3 )
v4 = StringVar()
e4 = Entry(root, textvariable = v4, state = 'readonly')
v4.set('Col4')
e4.grid(row = 1, column = 4 )
mainloop()
Related
I have copied the text file to excel sheet separating cells by ; delimiter.
I need to plot a chart using the same file which I achieved. Since all the values copied are type=str my chart gives me wrong points.
Please suggest to overcome this. Plot is should be made of int values
from datetime import date
from openpyxl import Workbook,load_workbook
from openpyxl.chart import (
LineChart,
Reference,
Series,
)
from openpyxl.chart.axis import DateAxis
excelfile = "C:\Users\lenovo\Desktop\how\openpychart.xlsx"
wb = Workbook()
ws = wb.active
f = open("C:\Users\lenovo\Desktop\sample.txt")
data = []
num = f.readlines()
for line in num:
line = line.split(";")
ws.append(line)
f.close()
wb.save(excelfile)
wb.close()
wb = load_workbook(excelfile, data_only=True)
ws = wb.active
c1 = LineChart()
c1.title = "Line Chart"
##c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'
data = Reference(ws, min_col=6, min_row=2, max_col=6, max_row=31)
series = Series(data, title='4th average')
c1.append(series)
data = Reference(ws, min_col=7, min_row=2, max_col=7, max_row=31)
series = Series(data, title='Defined Capacity')
c1.append(series)
##c1.add_data(data, titles_from_data=True)
# Style the lines
s1 = c1.series[0]
s1.marker.symbol = "triangle"
s1.marker.graphicalProperties.solidFill = "FF0000" # Marker filling
s1.marker.graphicalProperties.line.solidFill = "FF0000" # Marker outline
s1.graphicalProperties.line.noFill = True
s2 = c1.series[1]
s2.graphicalProperties.line.solidFill = "00AAAA"
s2.graphicalProperties.line.dashStyle = "sysDot"
s2.graphicalProperties.line.width = 100050 # width in EMUs
##s2 = c1.series[2]
##s2.smooth = True # Make the line smooth
ws.add_chart(c1, "A10")
##
##from copy import deepcopy
##stacked = deepcopy(c1)
##stacked.grouping = "stacked"
##stacked.title = "Stacked Line Chart"
##ws.add_chart(stacked, "A27")
##
##percent_stacked = deepcopy(c1)
##percent_stacked.grouping = "percentStacked"
##percent_stacked.title = "Percent Stacked Line Chart"
##ws.add_chart(percent_stacked, "A44")
##
### Chart with date axis
##c2 = LineChart()
##c2.title = "Date Axis"
##c2.style = 12
##c2.y_axis.title = "Size"
##c2.y_axis.crossAx = 500
##c2.x_axis = DateAxis(crossAx=100)
##c2.x_axis.number_format = 'd-mmm'
##c2.x_axis.majorTimeUnit = "days"
##c2.x_axis.title = "Date"
##
##c2.add_data(data, titles_from_data=True)
##dates = Reference(ws, min_col=1, min_row=2, max_row=7)
##c2.set_categories(dates)
##
##ws.add_chart(c2, "A61")
### setup and append the first series
##values = Reference(ws, (1, 1), (9, 1))
##series = Series(values, title="First series of values")
##chart.append(series)
##
### setup and append the second series
##values = Reference(ws, (1, 2), (9, 2))
##series = Series(values, title="Second series of values")
##chart.append(series)
##
##ws.add_chart(chart)
wb.save(excelfile)
wb.close()
I have modified below code in for loop and it worked.
f = open("C:\Users\lenovo\Desktop\sample.txt")
data = []
num = f.readlines()
for line in num:
line = line.split(";")
new_line=[]
for x in line:
if x.isdigit():
x=int(x)
new_line.append(x)
else:
new_line.append(x)
ws.append(new_line)
f.close()
wb.save(excelfile)
wb.close()
For each list,for each value check if its a digit, if yes converts to integer and store in another list.
Using x=map(int,x) didnt work since I have character values too.
I felt above is much more easy than using x=map(int,x) with try and Except
Thanks
Basha
My problem at the moment is I am trying to change a label(label 16) to the first value of entry_values[0] which isn't working I have tried passing it in as a variable and many other things, after about an hour of research I couldn't find a solution.I think the main problem is that it sets the label before the code with the entry is run so that it wont change. when I set it to a textvariable it produces an empty string (I think) but when I use just text it puts in a 0 where I expect my number.
def sub_menu(root):
global subpage
subpage = Frame(root)
button5 = Button(subpage, text="Save Generation Data",
command = lambda: save_entries())
button5.grid(row = 1, column = 6, sticky = E)
button6 = Button(subpage, text="Return To Main Page",
command = lambda: switch_page("main"))
button6.grid(row = 0, column = 6, sticky = W)
juveniles_label0 = Label(subpage,text="Juveniles")
adults_label1 = Label(subpage,text="Adults")
seniles_label2 = Label(subpage,text="Seniles")
population_label3 = Label(subpage,text="Population (Thousands)")
survival_rate_label4 = Label(subpage,text="Survival Rate (Between 0 and 1)")
birth_rate_label5 = Label(subpage,text="Birth Rate")
number_of_gens_label6 = Label(subpage,text="Number of Generations")
disease_trigger_label7 = Label(subpage,text="Disease Trigger Point")
global entry0
entry0 = Entry(subpage)
global entry1
entry1 = Entry(subpage)
global entry2
entry2 = Entry(subpage)
global entry3
entry3 = Entry(subpage)
global entry4
entry4 = Entry(subpage)
global entry5
entry5 = Entry(subpage)
global entry6
entry6 = Entry(subpage)
global entry7
entry7 = Entry(subpage)
global entry8
entry8 = Entry(subpage)
juveniles_label0.grid(row = 0, column = 1)
adults_label1.grid(row = 0, column = 2)
seniles_label2.grid(row = 0, column = 3)
population_label3.grid(row = 1, column = 0)
survival_rate_label4.grid(row = 2, column = 0)
birth_rate_label5.grid(row = 3, column = 0)
number_of_gens_label6.grid(row = 3, column = 2)
disease_trigger_label7.grid(row = 4, column = 0)
entry0.grid(row = 1, column = 1)
entry1.grid(row = 1, column = 2)
entry2.grid(row = 1, column = 3)
entry3.grid(row = 2, column = 1)
entry4.grid(row = 2, column = 2)
entry5.grid(row = 2, column = 3)
entry6.grid(row = 3, column = 1)
entry7.grid(row = 3, column = 3)
entry8.grid(row = 4, column = 1)
return subpage
def save_entries(): #entry recieve point
save_page = Frame(root)
""" if e0 < 0:
make a check to check if value is < 0 dont accept and if a value is inputed or not using if type(string_name) == str """
e0 = entry0.get()
if e0 >= 0:
entry_values[0] = (e0)
e1 = entry1.get()
if e0 >= 0:
entry_values[1] = (e1)
e2 = entry2.get()
if e0 >= 0:
entry_values[2] = (e2)
e3 = entry3.get()
if e0 >= 0:
entry_values[3] = (e3)
e4 = entry4.get()
if e0 >= 0:
entry_values[4] = (e4)
e5 = entry5.get()
if e0 >= 0:
entry_values[5] = (e5)
e6 = entry6.get()
if e0 >= 0:
entry_values[6] = (e6)
e7 = entry7.get()
if e0 >= 0:
entry_values[7] = (e7)
e8 = entry8.get()
if e0 >= 0:
entry_values[8] = (e8)
print entry_values
return save_page
def display_values(root):
sub2 = Frame(root)
global entry_values
label8 = Label(sub2, text = "Juveniles")
label9 = Label(sub2, text = "Adults")
label10 = Label(sub2, text = "Seniles")
label11 = Label(sub2, text = "Population(Thousands)")
label12 = Label(sub2, text = "Survival Rate(Between 1 and 0)")
label13 = Label(sub2, text = "Birth Rate")
label14 = Label(sub2, text = "Number of Generations")
label15 = Label(sub2, text = "Disase Trigger Point")
label16 = Label(sub2, text = entry_values[0])
label17 = Label(sub2, textvariable = entry_values[1])
label18 = Label(sub2, textvariable = "")
label19 = Label(sub2, textvariable = "")
label20 = Label(sub2, textvariable = "")
label21 = Label(sub2, textvariable = "")
label22 = Label(sub2, textvariable = "")
label23 = Label(sub2, textvariable = "")
label24 = Label(sub2, textvariable = "")
button7 = Button(sub2, text="Return To Main Page",
command = lambda: switch_page("main"))
label8.grid(row = 0, column = 1)
label9.grid(row = 0, column = 2)
label10.grid(row = 0, column = 3)
label11.grid(row = 1, column = 0)
label12.grid(row = 2, column = 0)
label13.grid(row = 3, column = 0)
label14.grid(row = 3, column = 3)
label15.grid(row = 4, column = 0)
label16.grid(row = 1, column = 1)
label17.grid(row = 1, column = 2)
label18.grid(row = 1, column = 3)
label19.grid(row = 2, column = 1)
label20.grid(row = 2, column = 2)
label21.grid(row = 2, column = 3)
label22.grid(row = 3, column = 1)
label23.grid(row = 3, column = 3)
label24.grid(row = 4, column = 1)
button7.grid(row = 0, column = 0)
return sub2
In order to change the text of a label you can do:
label["text"] = textVar
or
label.config(text=textVar)
So in your above code, when the entry changes, reconfigure the label using one of the above options.
I am getting the error need more than 1 value to unpack i am using defaultDict to make my list to dictionary.
def getTabsCols(request):
proj_name = request.GET.get('projname')
cursor = connection.cursor()
proj_details = TProjects.objects.get(
attr_project_name=proj_name, attr_project_type='Structure', attr_is_active=1)
pid = proj_details.project_id
query = 'call SP_Get_TABCOL_NAMES('+str(pid)+')'
cursor.execute(query)
proj_details = TProjects.objects.get(
attr_project_name=proj_name, attr_project_type='Structure', attr_is_active=1)
pid = proj_details.project_id
query = 'call SP_Get_TABCOL_NAMES('+str(pid)+')'
cursor.execute(query)
result = cursor.fetchall()
tab_list = []
tab_col_list = []
prd = {}
res = []
for row in result:
tabs = collections.OrderedDict()
schema_name = row[1]
table_name = row[3]
tab = (schema_name + '.' + table_name).encode('utf8')
tabs[tab] = row[5].encode('utf8')
tab_list.append(tabs)
d = collections.defaultdict(set)
for k, v in tab_list:
d1[k].append(v)
d = dict((k, tuple(v)) for k, v in d1.iteritems())
return HttpResponse(d, content_type="text/html")
I have 2 listboxes (which are connected so that items can move from one to the other) and at the end I would like to get all the entries in the second listbox by using a 'Ok' button (or simply closing the frame). I could add/remove values to a list every time an item is selected (as shown in the commented section of the code below) but I would rather have a single line of code along the lines of [master.selected.get(idx) for idx in master.selected.curselection()] in the close function but I am unable to get it working.
Code:
def measurementPopup(self,master):
self.chargeCarrier = StringVar()
self.massModifiers = StringVar()
self.chargeCarrier.set("[M+xH]")
def onselect1(evt):
w = evt.widget
index = int(w.curselection()[0])
value = w.get(index)
# My Dirty fix -> Here I could enter the selected value to a buffer list (to be returned in the ok function).
master.selected.insert(END,value)
master.avail.delete(index)
def onselect2(evt):
w = evt.widget
index = int(w.curselection()[0])
value = w.get(index)
# My Dirty fix -> Here I could remove the selected value from a buffer list (to be returned in the ok function).
master.selected.delete(index)
master.avail.insert(END,value)
def close(self):
# Here I would return the buffer list and close the window
master.measurementWindow = 0
top.destroy()
if master.measurementWindow == 1:
return
master.measurementWindow = 1
top = self.top = Toplevel()
top.protocol( "WM_DELETE_WINDOW", lambda: close(self))
self.charge = Label(top, text = "Charge", width = 10)
self.charge.grid(row = 0, column = 0, sticky = W)
self.min = Label(top, text = "Min", width = 5)
self.min.grid(row=0, column = 1, sticky = W)
self.minCharge = Spinbox(top, from_= 1, to = 3, width = 5)
self.minCharge.grid(row = 0, column = 2, sticky = W)
self.max = Label(top, text = "Max", width = 5)
self.max.grid(row = 0, column = 3, sticky = W)
self.maxCharge = Spinbox(top, from_ = 1, to=3, width=5)
self.maxCharge.grid(row = 0, column = 4, sticky = W)
self.chargeCarrier = OptionMenu(top, self.chargeCarrier, "[M+xH]", "[M+xNa]")
self.chargeCarrier.grid(row = 0, column = 5, sticky = W)
self.availMass = Label(top, text = "Available")
self.availMass.grid(row = 1, column = 1, sticky = W)
self.selectMass = Label(top, text = "Selected")
self.selectMass.grid(row = 1, column = 3, sticky = W)
self.massMod = Label(top, text = "Mass Mods")
self.massMod.grid(row = 2, column = 0, sticky = W)
self.avail = Listbox(top)
for i in UNITS:
if BLOCKS[i]['available'] == 1:
self.avail.insert(END,BLOCKS[i]['human_readable_name'])
self.avail.grid(row = 2, column = 1, columnspan = 2, sticky = W)
self.avail.bind('<<ListboxSelect>>',onselect1)
self.selected = Listbox(top)
self.selected.grid(row = 2, column = 3, columnspan = 2, sticky = W)
self.selected.bind('<<ListboxSelect>>',onselect2)
self.ok = Button(top,text = 'Ok',command = lambda: close(self))
self.ok.grid(row = 3, column = 0, sticky = W)
I have tried to use the following small snippet in the close function:
values = [master.selected.get(idx) for idx in master.selected.curselection()]
print ', '.join(values)
However, the for segment doesn't return anything. I would expect that this is due to the fact that nothing is actually selected but that I would need something opposite, along the lines of master.selected.allitems() (if it exists and if I understand it correctly).
Summary
How would one get all the items in 1 specific listbox?
The .get() function for the Listbox widget allows you to specify a range of items, which can be specified as 0 to END to return a tuple of all the items.
Example:
from Tkinter import *
root = Tk()
l = Listbox(root, width = 15)
l.pack()
l.insert(END, "Hello")
l.insert(END, "world")
l.insert(END, "here")
l.insert(END, "is")
l.insert(END, "an")
l.insert(END, "example")
def close():
global l, root
items = l.get(0, END)
print(items)
root.destroy()
b = Button(root, text = "OK", command = close).pack()
root.mainloop()
I hope this helps, if it's not what you were looking for let me know in a comment and I can try expand my answer.
from Tkinter import *
import tkFileDialog
import sys
def openwindows():
eText.set(open.show())
def loadfiles():
filedirectory=eText.get()
f=file(filedirectory)
lines = f.readlines()
length=len(lines)
#vertex=[]
#face=[]
for x in range(0,length):
print (x)
Root=Tk()
RTitle=Root.title("Assignment_2")
RWidth=Root.winfo_screenwidth()
RHeight=Root.winfo_screenheight()
Root.geometry("%dx%d+%d+%d" % (RWidth/3,RHeight/3*2,RWidth/2-RWidth/6, RHeight/2-RHeight/3))
frame1 = Frame(Root)
frame1.pack()
Label(frame1, text="Filename: ").pack(side = LEFT)
eText = StringVar()
statusbar = Entry(frame1, state="readonly", textvariable=eText)
eText.set("pyramid.txt")
statusbar.pack(side = LEFT)
myfiletypes = [('text files', '*.txt'), ('All files', '*')]
open = tkFileDialog.Open(frame1, filetypes = myfiletypes)
Button(frame1, text="Browse", fg = "Blue", command=openwindows).pack(side = LEFT)
Button(frame1, text = "Load", fg = "red", command=loadfiles).pack(side=LEFT)
frame2 = Frame(Root)
frame2.pack()
Label(frame2, text="Rotation Axis: ").pack(side=LEFT)
r1 = IntVar()
r1.set(3)
Radiobutton(frame2, text="X", variable=r1, value=1).pack(side=LEFT)
Radiobutton(frame2, text="Y", variable=r1, value=2).pack(side=LEFT)
Radiobutton(frame2, text="Z", variable=r1, value=3).pack(side=LEFT)
Radiobutton(frame2, text="Line AB", variable=r1, value=4).pack(side=LEFT)
Label(frame2, text="A:").pack(side=LEFT)
pointA = Entry(frame2, width=10)
pointA.insert(0, "[0.0,0.0,0.0]")
pointA.pack(side=LEFT)
Label(frame2, text="B:").pack(side=LEFT)
pointB = Entry(frame2, width=10)
pointB.insert(0, "[1.0,1.0,1.0]")
pointB.pack(side=LEFT)
var1 = StringVar(Root)
var1.set("90")
Label(frame2, text="Degree:").pack(side=LEFT)
Spinbox(frame2, width=3, from_=0, to=350, increment=10, textvariable=var1).pack(side=LEFT)
var2 = StringVar(Root)
var2.set("5")
Label(frame2, text="Steps:").pack(side=LEFT)
Spinbox(frame2, width=3, from_= 1, to = 10, textvariable=var2).pack(side=LEFT)
Button(frame2, text = "Rotate", fg = "Blue").pack(side=LEFT)
frame3 = Frame(Root)
frame3.pack()
Label(frame3, text="Scale Ratio: ").pack(side=LEFT)
r2 = IntVar()
r2.set(1)
Radiobutton(frame3, text="All", variable=r2, value=1).pack(side=LEFT)
var3 = StringVar(Root)
var3.set("1")
Spinbox(frame3, width=4, from_= 0.25, to = 4, textvariable=var3, increment=0.25, format='%3.2f').pack(side=LEFT)
Radiobutton(frame3, text="[Sx,Sy,Sz]", variable=r2, value=2).pack(side=LEFT)
scaleamount = Entry(frame3, width=10)
scaleamount.insert(0, "[1,1,1]")
scaleamount.pack(side=LEFT)
var4 = StringVar(Root)
var4.set("4")
Label(frame3, text="Steps:").pack(side=LEFT)
Spinbox(frame3, width=3, from_= 1, to = 10, textvariable=var4).pack(side=LEFT)
Button(frame3, text = "Scale", fg = "Blue").pack(side=LEFT)
separator = Frame(height=2, bd=1, relief=SUNKEN)
separator.pack(fill=X, padx=5, pady=5)
Canvas(Root, bg="Blue").pack(fill="both", expand=True)
mainloop()
when you click Load, its suppose to read the file and display this.
0
1
2
3
4
5
6
7
.....
whatever number of lined you have in the txt file.
But the problem is that, it only displays this
0
which means the for loop only ran one time and thats it.
if I close the program, then the rest of the numbers will show in the command prompt.
really weird... doesnt make sense.
the loadfiles() itself works perfectly fine.
It just wont work with the button
The issue has to do with how the UI is processing.
Try replacing print (x) with
sys.stdout.write(str(x) + '\n') # or "{}\n".format(x)
sys.stdout.flush()
If you search for tkinter and flush you will find plenty of conversations to read.