-1
import Tkinter
import tkMessageBox
import xlwt
from xlrd import open_workbook
from tempfile import TemporaryFile
from xlwt import Workbook
import tkSimpleDialog
import tkMessageBox
from tkFileDialog import *
top = Tkinter.Tk()
#FSR
def fsr():
   tkMessageBox.showinfo("Open", "Select Feedback file")
   filename1 = askopenfilename(filetypes=[("Excel worksheets","*.xls")])
   if filename1=='':
         tkMessageBox.showerror('Error', 'All files must be uploaded')
         fsr()
B1 = Tkinter.Button(top, text = "Details upload", command = fsr)
B1.pack()
top.mainloop()
wb1 = open_workbook(filename1)
sheet1 = wb1.sheet_by_index(0)
batch = []
print "****"
for row in range(sheet1.nrows):
    if(row!=0):
        batch.append(sheet1.cell(row,16).value)
        print "****"

This code snippet does not print anything on console. The print "****" is not working. And also, if top.mainloop() is emitted, the tk window is not showing. How to solve this?

Jonah Fleming
  • 1,187
  • 4
  • 19
  • 31
  • 2
    Fix your indentation. Your code is unrunnable. – khelwood Nov 07 '15 at 21:26
  • The indentation is correct in actual code. Probably messed up while copying here. Kindly tell what is the problem with the code. – user3848981 Nov 07 '15 at 21:29
  • Corrected the indentation as per your advice. Please check now.. – user3848981 Nov 07 '15 at 21:41
  • Your print statement works for me. It won't work until you destroy the window of course, but after that it works just fine. Are you aware that `mainloop` won't return until the widget is destroyed, so no code after it will run until it returns? – Bryan Oakley Nov 07 '15 at 22:30
  • Yes. I know that. But how to make the code work which is written after mainloop() function? I am new to xlrd and Tinker. Thats why I am unable to understand. Kindly help. – user3848981 Nov 07 '15 at 22:35

1 Answers1

1

Put code in fsr like this:

import Tkinter
import tkMessageBox
import xlwt
from xlrd import open_workbook
from tempfile import TemporaryFile
from xlwt import Workbook
import tkSimpleDialog
import tkMessageBox
from tkFileDialog import *

#FSR
def fsr():
    while True:
        tkMessageBox.showinfo("Open", "Select Feedback file")
        filename1 = askopenfilename(filetypes=[("Excel worksheets","*.xls")])
        if filename1:
            break
        tkMessageBox.showerror('Error', 'All files must be uploaded')

    wb1 = open_workbook(filename1)
    sheet1 = wb1.sheet_by_index(0)
    batch = []
    print "****"
    for row in range(sheet1.nrows):
        if(row!=0):
            batch.append(sheet1.cell(row,16).value)
            print "****"

top = Tkinter.Tk()
B1 = Tkinter.Button(top, text="Details upload", command=fsr)
B1.pack()
top.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148